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

别来无恙 提交于 2019-12-10 02:28:07

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12009089/why-does-git-say-i-am-40-commits-ahead-when-i-seem-up-to-date-and-a-push-pull-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!