git 撤消快进合并
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14308580/
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
Undo a fast-forward merge
提问by Fabian Tamp
I've made some changes to my git repo that I wish to undo.
我对我的 git repo 做了一些我希望撤消的更改。
My git repo looked like this:
我的 git repo 看起来像这样:
A-B---- master
\ /
C-D * develop
I was on the develop
branch, forgot that it differed from the master
branch, made a change on develop
, merged it into master
, and then pushed to my remote (called publish
).
我在develop
分支上,忘记了它与master
分支不同,对进行了更改develop
,将其合并到master
,然后推送到我的遥控器(称为publish
)。
Because there were no changes on master since B (common ancestor), git did a fast-forward merge.
因为自 B(共同祖先)以来 master 上没有任何变化,所以 git 进行了快进合并。
Now, my repo looks like this:
现在,我的回购看起来像这样:
A-B-C-D master, develop, remotes/publish/master, remotes/publish/develop.
I wanted to revert the last merge, restoring master
to B.
我想恢复上次合并,恢复master
到 B。
From what I read in How to undo last commit(s) in Git?, I used git reset sha-of-B
to restore my master
branch to revision B.
从我在How to undo last commit(s) in Git? 中读到的内容?,我曾经git reset sha-of-B
将我的master
分支恢复到修订版 B。
Questions:
问题:
- How do I restore
develop
to revision D? - How do I then push these changes back to remote/publish?
- 如何恢复
develop
到版本 D? - 然后我如何将这些更改推送回远程/发布?
回答by Simon Boudrias
If you reset the branch master, it won't touch develop branch. In order to reput all in order, you should do:
如果您重置分支 master,它不会触及开发分支。为了声名狼藉,你应该这样做:
git checkout master
git reset --hard sha-of-B
git checkout develop
git reset --hard sha-of-D
git checkout master
git merge develop --no-ff
回答by Robert Siemer
- imagine you are still on masterwhere you merged and pushed
git reset --hard @{1}
- this resets branch “master”to where it was one step back on your computer (i.e. at “B”)
- for developdo nothing, because it should still be at “D”
git push publish develop
- this pushes branch “develop”in the usual way
git push publish master --force-with-lease
- pushes branch “master”backwards, with the permission gained via
--force-with-lease
in a safe way - the use of
--force
might silentlyoverwrite work from others, so I never use that
- pushes branch “master”backwards, with the permission gained via
- or both push operations together with this surprising syntax:
git push publish develop --force-with-lease=master
- 想象一下你仍然在你合并和推送的master上
git reset --hard @{1}
- 这会将分支“master”重置为它在您的计算机上后退一步的位置(即“B”)
- 因为开发什么都不做,因为它应该仍然在“D”
git push publish develop
- 这以通常的方式推动分支“发展”
git push publish master --force-with-lease
- 向后推动分支“master”,以
--force-with-lease
安全的方式获得许可 - 使用
--force
可能会默默地覆盖其他人的工作,所以我从不使用它
- 向后推动分支“master”,以
- 或者同时使用这种令人惊讶的语法进行推送操作:
git push publish develop --force-with-lease=master
回答by mvp
If seems that you were checked out to develop
when you ran git reset
, and this is what messed you up.
如果您似乎develop
在跑步时被签出git reset
,这就是让您陷入困境的原因。
You can fix develop
as follows:
您可以develop
按如下方式修复:
git checkout develop
git merge --ff-only D
Now, you can restore master
back to B:
现在,您可以恢复master
回 B:
git checkout master
git reset --hard B
Get back to develop
and push it:
返回develop
并推送它:
git checkout develop
git push develop