22

I am learning about working with Git remotes by reading the relevant section of the Pro Git Book.

If you clone a repository, the command automatically adds that remote repository under the name "origin". So, git fetch origin fetches any new work that has been pushed to that server since you cloned (or last fetched from) it.

It’s important to note that the git fetch command only fetches the data to your local repository; it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.

Here is what I tried. I cloned a repository and edited a file. In the original repository, someone updated the same file and pushed. Then,

  1. I ran git fetch. It showed some update progress message. However, git log did not show that update. Did I misunderstand what git fetch does? Am I missing something?

  2. I ran git pull, and I got

error: Your local changes to 'hello_world.c' would be overwritten by merge. Aborting. Please, commit your changes or stash them before you can merge.

Here, I believe it's also merging and to avoid accidental data loss, it aborts.

Edit: Thanks for the answers. Actually before looking at the answers, I was trying myself and realized the same with the following commands / outputs:

$ git ls-remote origin
d0006a6bfa95e0e90aa820a0e50d31a548625652    HEAD
d0006a6bfa95e0e90aa820a0e50d31a548625652    refs/heads/master
$ git ls-remote .
14375458b8a6b84f82d9fa4d2ded0bb8c9e87431    HEAD
14375458b8a6b84f82d9fa4d2ded0bb8c9e87431    refs/heads/master
d0006a6bfa95e0e90aa820a0e50d31a548625652    refs/remotes/origin/HEAD
d0006a6bfa95e0e90aa820a0e50d31a548625652    refs/remotes/origin/master

Also with following commands:

$git log origin --oneline
$git log --oneline

Thank you for bearing with my stupid questions ;-)

Adil
  • 2,418
  • 7
  • 34
  • 38
  • 1
    *[...] git fetch command pulls the data to your local repository [...]* Careful with the term "pull"; it has a very specific meaning in Git, and using it the way you do can only lead to confusion. – jub0bs Jul 02 '15 at 08:04

4 Answers4

22

By default, git log shows the log of the current branch. Since fetching without merging doesn't change anything in your current (local) branch, the git log output doesn't change.

git log can take the --all option to show the history of all branches, local and remote. Alternatively, you can explicitly list the remote branch as in git log origin/master.

7

You wrote

However, git log did not show that update. Did I misunderstand what git fetch does? Am I missing something?

You seem to be confused about what both git log and git fetch do.

Under the assumption that a branch called master is currently checked out—you may also be in detached-HEAD state, but let's keep things simple, for the sake of this explanation—the command git log, without any other arguments, is equivalent to git log master. This command tells Git

Show me all the commits that are in the ancestry of the local master branch.

However, you need to understand that git fetch origin doesn't affect/update your local branches, such as master. Instead, new commits from origin (or any other remote you fetch from) are put in remote-tracking branches.

Remote-tracking branches are special local branches whose sole purpose is to reflect the state of branches that live in a remote repository at the time of your last communication with the server. In particular, you can't commit to branches of this type.

What you probably want is to run

git fetch origin

to fetch all new commits from origin and puts them in remote-tracking branches (including origin/master, here), and then

git log master..origin/master

to show a log of all the "new" commits that were added on the remote master branch, i.e. the branch that lives on the remote server you know as origin, which your remote-tracking branch origin/master keeps track of.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
4

After git fetch, your local repo knows the changes from the remote repo but hasn’t applied them to your local branches yet.

git log without additional parameters shows the log of the current branch – and you haven’t merged the remote changes into that branch yet.

git pull does git fetch and git merge FETCH_HEAD.

Your error message means that there are uncommitted changes in your local repo. You can commit or stash them (as the message states) and try merge (or pull) again.

Melebius
  • 6,183
  • 4
  • 39
  • 52
1

Your local branches never get updated from a fetch, only the tracking branches do, git pull on the other hand does,

git pull = git fetch + git merge

You can't see any updates in git log simply beause you are running it on the wrong branch, to see your changes from just a git fetch then you need to git log on the right branch, which in your case would be

git log origin/master
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89