git pull origin master does not update origin/master?

Kenneth Baltrinic picture Kenneth Baltrinic · Dec 31, 2011 · Viewed 47.4k times · Source

According to the documentation, git pull performs a git fetch then a git merge, however in that case performing git pull origin master should perform a git fetch origin master right? However, it does not appear to be doing so. Here is an example.

Supposed my remote origin master (on GitHub in my case) has the following history:

commit 1111111 : my first commit
commit 2222222 : a commit from someone else

and I only have my first commit locally as doing following shows

git checkout master
git log --pretty=format:'%h' -n 1
1111111

git checkout origin/master
git log --pretty=format:'%h' -n 1
1111111

From here I do my pull and look at the results as follows:

git checkout master
git pull origin master

git log --pretty=format:'%h' -n 1
2222222

git checkout origin/master
git log --pretty=format:'%h' -n 1
1111111

As can be seen, the pull did in fact update my master branch with the new commit(s) from the remote origin, but my local origin/master is still where it was. Forcing me to do the following

git fetch origin master

git checkout origin/master
git log --pretty=format:'%h' -n 1
2222222

Is this correct behavior for git pull or might I have something miss configured? I looked through the git pull man page and didn't see anything that suggested this but I may have missed it.

Answer

Cascabel picture Cascabel · Dec 31, 2011

It's a bit weird, but if you use git pull [remote] <refspec> it actually doesn't update the remote refs. It sort of makes sense if you think about it a certain way: since you're specifying a specific ref to fetch, it doesn't have to look up anything about your remote branches, so it doesn't inherently know what remote branch it should update. It of course could figure it out, and I wouldn't be surprised if it gets fixed eventually, but that's the existing behavior. (There may be messages on the mailing list about it - I don't know.)

You can easily work around it, though. If you use git pull origin/master, since you're specifying what to fetch via a remote branch, it should update that remote branch. And if you're on your master branch anyway (or any other branch tracking origin/master), you can just do git pull and let it fill in the defaults, and it will update remote branches.

This is documented in the git-pull man page, most concisely under EXAMPLES but also elsewhere. The relevant part:

Merge into the current branch the remote branch next:

$ git pull origin next

This leaves a copy of next temporarily in FETCH_HEAD, but does not update any remote-tracking branches. Using remote-tracking branches, the same can be done by invoking fetch and merge:

$ git fetch origin
$ git merge origin/next