为什么 Git 向我显示这个本地未推送的提交?

Why does Git show me this local unpushed commits?

我有一个奇怪的行为,我不明白可能是什么原因。

基本上:

  1. 我在 NetBeans 中进行本地开发
  2. 然后推送到 Bitbucket
  3. 最后,从生产服务器拉取

所以,我从不从生产服务器提交,我只拉到那里。

所以在我完成最后一次拉动之后:

-bash-4.2$ git pull origin master

然后我查了一下状态,给了我这个:

-bash-4.2$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#   (use "git push" to publish your local commits)

然后我去看看那些是什么提交:

-bash-4.2$ git cherry -v
+ 2456db712fafab4b845a128711820ca107475e1f Remove comment added by josepluis to yiic horde loader
+ 8b9edf42c0d5177cd77f061160e27580098b745c Fix small bug when ordering pictures that was executing the update on wrong table

如果我检查 Bitbucket,我会看到这些不是从生产服务器进行的本地提交,而是我从我的计算机上完成并推送到 Bitbucket 的提交。

那么为什么生产服务器的 Git 考虑那些提交,本地未推送的提交?

尝试检查 origin/master 的 reflog,并检查你的两次拉取之间是否有历史记录重写:

$ git reflog origin/master
ffac48d09 refs/remotes/origin/master@{0}: fetch: fast-forward
d7dffce1c refs/remotes/origin/master@{1}: fetch: fast-forward
e05806da9 refs/remotes/origin/master@{2}: fetch: fast-forward
aeddbfdfa refs/remotes/origin/master@{3}: fetch: fast-forward
...

$ git log --oneline --graph ffac48d09 d7dffce1c e05806da9
# and check if the sequence of commits is linear, or if it "forked" at some point

两种避免被重写(和git pull的自动合并或变基)影响的方法:

  1. 停止使用 git pull、运行 git fetch,检查 origin/master 是如何进化的,这取决于您的需要:
    • git merge origin/master / git rebase origin/master : 运行 与 git pull 相同的操作,但 被告知历史已更改
    • git reset --hard origin/master:不要尝试合并,只需使用这个特定的提交
  2. 告诉git pull只快进:git pull --ff-only