Git:如何“撤消”合并
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4434787/
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: How to "undo" a merge
提问by Jake
The situation:
Starting with the Master at A
I branched and made some changes at B
then merged that branch back in (C)
. After making some more changes I was at D
but found I needed to deploy the code without the changes that happened in the branch. If I hand't merged it that would have been fine.
情况:从 Master 开始,A
我分支并进行了一些更改,B
然后将该分支合并回(C)
. 在进行了更多更改后,我D
发现我需要部署代码,而不需要在分支中发生更改。如果我不手动合并它就可以了。
A_______C___D
\ /
\_B_/
Firstly I'd like to know what I should have done from here to deploy the code as if the merge never happened. Note: none of the same files that were edited in the branch have been edited in the master.
首先,我想知道我应该从这里做什么来部署代码,就好像合并从未发生过一样。注意:在分支中编辑的相同文件都没有在 master 中编辑过。
Secondly...
其次...
I didn't have time to work out the best method to deal with this, so I deleted the files which the branch added, and manually reverted a few edits made in the branch then commited the result for deployment (F
)
我没有时间想出最好的方法来解决这个问题,所以我删除了分支添加的文件,并手动恢复了在分支中所做的一些编辑,然后提交了部署结果(F
)
A_______C___D___F
\ /
\_B_/
I want to be able to keep developing the branch and merge any changes from the master into it to keep it up to date, but if I do that the stuff I did to create F
will be merged in and cause the files to be deleted and the edits to be reverted. What is the best way to deal with this?
我希望能够继续开发分支并将 master 的任何更改合并到它以使其保持最新状态,但是如果我这样做,我所做的创建的东西F
将被合并并导致文件被删除,并且要恢复的编辑。处理这个问题的最佳方法是什么?
采纳答案by Gauthier
You can use rebase to do that in one step:
您可以使用 rebase 一步完成此操作:
git rebase --onto A C D
I just tested this, with appropriate results:
我刚刚对此进行了测试,并得到了适当的结果:
$ edit test.txt
$ git add .
$ git commit -mA
$ git checkout -b the_branch
$ edit test.txt
$ git commit -a -mB
$ git checkout master
$ git merge master the_branch --no-ff
$ edit test.txt
$ git commit -a -mD
From here you have the situation you described. Then:
从这里你有你描述的情况。然后:
$ git rebase --onto <SHA1-for-A> <SHA1-for-C> master
rebases commits from C (excluded) to master, onto A. I needed to fix some conflicts since I modified at the same places in B and D, but I think you won't.
将 C(排除)提交到 master 的 rebase 提交到 A。我需要修复一些冲突,因为我在 B 和 D 的相同位置进行了修改,但我认为你不会。
_D'
/
/
A_______C___D
\ /
\_B_/
Doc about git rebase --onto
, which is more or less your situation:
http://git-scm.com/docs/git-rebase
Doc about git rebase --onto
,这或多或少是你的情况:http:
//git-scm.com/docs/git-rebase
If you had:
如果你有:
A_______C___D___F
\ /
\_B_/
, then you have now:
,那么你现在有:
_D'___F'_(master)
/
/
A_______C___D___F
\ /
\_B_/(the_branch)
From here, merging the changes in master into the branch is easy. Discard the commit F'
altogether.
从这里开始,将 master 中的更改合并到分支中很容易。F'
完全放弃提交。
$ git checkout master # if you were not here already
$ git branch old_fix # if you want to be able to return to F' later
$ git reset --hard <SHA1-to-D'>
After the commands above you have:
在上面的命令之后,你有:
(master)
/
_D'___F'_(old_fix)
/
/
A_______C___D___F
\ /
\_B_/(the_branch)
To merge updates of master into the_branch:
将 master 的更新合并到 the_branch:
$ git checkout the_branch
$ git merge master
... and fix the conflicts.
...并解决冲突。
回答by terminus
The obvious solution is to reset to A
, reapply all patches manually and resolve conflicts (which you won't have).
显而易见的解决方案是重置为A
,手动重新应用所有补丁并解决冲突(您不会有)。
Alternatively you can just git revert
patch B
but that will create a new commit.
或者,您可以只打git revert
补丁,B
但这会创建一个新的提交。
Although Gauthier's answer is better.
虽然 Gauthier 的答案更好。