Git 说本地分支位于远程分支之后,但实际上并非如此

场景:

  1. 我做了一个新的分支
  2. 黑进去
  3. 承认吧
  4. 按下去
  5. 再修改一下
  6. 再次承诺
  7. 再用力

Git 回应道:

由于当前分支的提示落后,更新被拒绝 它遥远的对应物等等。

我是唯一一个黑进这根树枝的人——没有其他人碰过它。远程分支实际上是 后面本地分支。我根本不用拉。

(如果我拉,Git 会报告两者之间的冲突,并迫使我将分支合并到它自己中)

为什么(可能)会发生这种情况? 我如何诊断/修复它?

需要说明的是,我没有在任何地方进行分支,而 没别人了正在进行这方面的工作:

Remote: Commit A -------- Commit B


Local:  Commit A -------- Commit B -------- Commit C

C 是 B 的直接延伸,没有分支。但是 git 认为 C 是 A 的一个分支:

Remote: Commit A -------- Commit B


------- Commit C
/
Local:  Commit A -------- Commit B

不是,它是 B 的直接延续。

174509 次浏览

You probably did some history rewriting? Your local branch diverged from the one on the server. Run this command to get a better understanding of what happened:

gitk HEAD @{u}

I would strongly recommend you try to understand where this error is coming from. To fix it, simply run:

git push -f

The -f makes this a “forced push” and overwrites the branch on the server. That is very dangerous when you are working in team. But since you are on your own and sure that your local state is correct this should be fine. You risk losing commit history if that is not the case.

This happened to me when I was trying to push the develop branch (I am using git flow). Someone had push updates to master. to fix it I did:

git co master
git pull

Which fetched those changes. Then,

git co develop
git pull

Which didn't do anything. I think the develop branch already pushed despite the error message. Everything is up to date now and no errors.

To diagnose it, follow this answer.

But to fix it, knowing you are the only one changing it, do:
1 - backup your project (I did only the files on git, ./src folder)
2 - git pull
3 - restore you backup over the many "messed" files (with merge indicators)

I tried git pull -s recursive -X ours but didnt work the way I wanted, it could be an option tho, but backup first!!!

Make sure the differences/changes (at git gui) are none. This is my case, there is nothing to merge at all, but github keeps saying I should merge...

The solution is very simple and worked for me.

Try this :

git pull --rebase <url>

then

git push -u origin master