Git 说本地分支在远程分支后面,但事实并非如此
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12650261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git says local branch is behind remote branch, but it's not
提问by Tim Janke
Scenario:
设想:
- I make a new branch
- hack on it
- commit it
- push it
- hack on it some more
- commit again
- try to push again
- 我创建了一个新分支
- 破解它
- 提交它
- 推它
- 再破解一下
- 再次提交
- 尝试再次推动
Git responds:
吉特回应:
Updates were rejected because the tip of your current branch is behind its remote counterpart. etc.
更新被拒绝,因为您当前分支的尖端落后于其远程对应分支。等等。
I'm the only one hacking on this branch - no one else is touching it. The remote branch is actually behindthe local branch. I shouldn't have to pull at all.
我是这个分支上唯一一个黑客 - 没有其他人接触它。远程分支实际上在本地分支后面。我根本不应该拉。
(And if I do pull, Git reports conflicts between the two, and forces me to merge the branch into itself)
(如果我拉,Git 报告两者之间的冲突,并迫使我将分支合并到自身中)
Why is this (likely) happening? And how can I diagnose/fix it?
为什么这(可能)会发生?我该如何诊断/修复它?
To be clear, I'm not branching anywhere, and no one elseis working on it:
需要明确的是,我没有在任何地方分支,也没有其他人在处理它:
Remote: Commit A -------- Commit B
Local: Commit A -------- Commit B -------- Commit C
C is a straight continuation of B, no branching involved. But git thinks C is a branch of A:
C 是 B 的直接延续,不涉及分支。但是 git 认为 C 是 A 的一个分支:
Remote: Commit A -------- Commit B
------- Commit C
/
Local: Commit A -------- Commit B
It's not; it's a straight continuation of B.
它不是; 它是 B 的直接延续。
回答by Chronial
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 overwritesthe 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.
这-f
使其成为“强制推送”并覆盖服务器上的分支。当你在团队中工作时,这是非常危险的。但是由于您是一个人并且确定您当地的状态是正确的,所以这应该没问题。如果情况并非如此,您可能会丢失提交历史记录。
回答by theRana
The solution is very simple and worked for me.
解决方案非常简单,对我有用。
Try this :
尝试这个 :
git pull --rebase <url>
then
然后
git push -u origin master
回答by Daniel
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 flow)。有人向 master 推送更新。修复它我做了:
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.
这没有做任何事情。我认为尽管有错误消息,但开发分支已经推送了。现在一切都是最新的,没有错误。
回答by Aquarius Power
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)
但是要修复它,知道您是唯一一个更改它的人,请执行以下操作:
1 - 备份您的项目(我只做了 git, ./src 文件夹中的文件)
2 - git pull
3 - 恢复您对许多“混乱”文件的备份(带合并指标)
I tried git pull -s recursive -X ours
but didnt work the way I wanted, it could be an option tho, but backup first!!!
我试过了,git pull -s recursive -X ours
但没有按照我想要的方式工作,这可能是一个选择,但先备份!!!
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...
确保差异/更改(在 git gui 处)没有。这是我的情况,根本没有什么可以合并的,但是github一直说我应该合并...