Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git say I am 40 commits ahead when I seem up to date and a push-pull (no files) fixes it?

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?

like image 635
Michael Durrant Avatar asked Aug 17 '12 15:08

Michael Durrant


2 Answers

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.

like image 89
Jamund Ferguson Avatar answered Oct 19 '22 18:10

Jamund Ferguson


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.

like image 21
Werner Smit Avatar answered Oct 19 '22 18:10

Werner Smit