11

I switch to master and it says I am ahead by 40 commits:

$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 40 commits.

But when I then do a pull it says I am up-to-date:

$ git pull origin master
From https://github.com/dmcouncil/dmWorkflow
 * branch            master     -> FETCH_HEAD
Already up-to-date.

However I can resolve this (remove the 40 commits msg) with:

Michaels-MacBook-Pro-2:dmWorkflow durrantm$ git push origin master
Everything up-to-date

and now the '40 commits' message is gone:

$ git status
# On branch mdd_play_settings_and_topics_reports
nothing to commit (working directory clean)

Why do I have to do the extra push (of what seems like nothing) to get in sync?
Is there a better approach I can take to this?

Aaron McDaid
  • 24,484
  • 9
  • 56
  • 82
Michael Durrant
  • 84,444
  • 83
  • 284
  • 429
  • I changed your title. You said 'behind' when you meant to say 'ahead'. – Aaron McDaid Aug 17 '12 at 17:01
  • Also, you started on branch 'master', but at the end you are on branch 'mdd_play_settings_and_topics_reports'. Please explain this. – Aaron McDaid Aug 17 '12 at 17:02
  • Can you confirm that the '40 commits ahead' message is still printed after the pull and before the push? – Aaron McDaid Aug 17 '12 at 17:26
  • Hi Aaron, yes it is, i.e. the `git pull` says I am up to date but then when I do `git status` it shows the 40 commits ahead message. It's resolved now for this one but this is the pattern I've seen in the past with `git pull` and `git status` messages – Michael Durrant Aug 17 '12 at 19:41
  • I've also experienced this problem and believe it is a bug in git. None of these answers are actually addressing abnormal behavior, but are simply explaining how git works. – Colleen Jun 12 '13 at 17:19

2 Answers2

6

This means that your local information about origin/master is different from the remote version. git fetch will correct that. git pull works in your case, because it also does a git fetch.

Jamund Ferguson
  • 15,537
  • 3
  • 40
  • 49
  • This makes the most sense to me. – Michael Durrant Aug 17 '12 at 17:12
  • 3
    This answer does not seem relevant for this particular question. The local master is *ahead* of origin/master. Hence, a pull does nothing. My reading of the question is that the '40 commits ahead' message is still present after the pull. And that the message only disappears after the push (as expected). – Aaron McDaid Aug 17 '12 at 17:24
  • Hi Aaron, yes the way you've described it is correct. Hmmm, I'm goona uncheck this answer (Sorry Jamund) as this is probably not the answer to my question. – Michael Durrant Aug 17 '12 at 19:42
  • No problem! Hope we can get to the bottom of it! – Jamund Ferguson Aug 17 '12 at 20:24
  • @JamundFerguson I did a `git pull origin master` and it said I was ahead by 6 commits. I then did a `git fetch` and it said I was current. So it seemed your solution did work for me... – Matt Oct 22 '13 at 21:14
  • I had this same issue, git pull did not resolve it (actually it increased the number of commits I was ahead by). Doing a git fetch did however resolve it, note that nothing changed on the server O_o – Derokorian Apr 04 '14 at 13:59
3

Just adding to Jamund Ferguson's answer...

If you have multiple remotes configured you could also do a git remote update which will fetch the information for all the remotes.

As with git fetch this will only update the information you have about the state of the remotes. It will not update or merge any code, so it is recommended to do it often to avoid strange status reports and incorrect diffs.

As a rule of thumb you would be referring to the local information of the remote branch when you use a command that uses a slash instead of a space after the remote eg. git command origin/master and git command origin master

Usage example: git checkout origin/master will switch to the code as per your current local information and git diff origin/master will diff your current code with your current local information about the remote branch. This means you could easily create a branch or diff your code with an outdated information/code of the remote if you don't fetch regularly which can lead to cumbersome issues.

Werner Smit
  • 1,571
  • 1
  • 10
  • 9