Git:如何反向合并提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1809484/
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 reverse-merge a commit?
提问by Mot
With SVN it is easy to reverse-merge a commit, but how to do that with Git?
使用 SVN 很容易反向合并提交,但如何使用 Git 做到这一点?
采纳答案by Ben James
To create a new commit that 'undoes' the changes of a past commit, use:
要创建“撤消”过去提交的更改的新提交,请使用:
$ git revert <commit>
It's also possible to actually remove a commit from an arbitrary point in the past by rebasing and then resetting, but you really don't want to do that if you have already pushed your commits to another repository (or someone else has pulled from you).
也可以通过变基然后重置从过去的任意点实际删除提交,但是如果您已经将提交推送到另一个存储库(或其他人已从您那里提取),您真的不想这样做.
回答by Marcus Ericsson
To revert a mergecommit, you need to use: git revert -m <parent number>
. So for example, to revert the recent most merge commit using the parent with number 1 you would use:
要恢复一个合并提交,你需要使用:git revert -m <parent number>
。因此,例如,要使用编号为 1 的父级还原最近的合并提交,您可以使用:
git revert -m 1 HEAD
To revert a merge commit before the last commit, you would do:
要在上次提交之前恢复合并提交,您可以执行以下操作:
git revert -m 1 HEAD^
Use git show <merge commit SHA1>
to see the parents, the numbering is the order they appear e.g. Merge: e4c54b3 4725ad2
使用git show <merge commit SHA1>
见到父母,编号是他们出现如顺序Merge: e4c54b3 4725ad2
git merge documentation: http://schacon.github.com/git/git-merge.html
git 合并文档:http: //schacon.github.com/git/git-merge.html
git merge discussion (confusing but very detailed): http://schacon.github.com/git/howto/revert-a-faulty-merge.txt
git 合并讨论(令人困惑但非常详细):http: //schacon.github.com/git/howto/revert-a-faulty-merge.txt
回答by Rudedog
If I understand you correctly, you're talking about doing a
如果我理解正确的话,你说的是做一个
svn merge -rn:n-1
to back out of an earlier commit, in which case, you're probably looking for
退出较早的提交,在这种情况下,您可能正在寻找
git revert
回答by kapil chakravarthy
git reset --hard HEAD^
Use the above command to revert merge changes.
使用上述命令恢复合并更改。
回答by rkrisztian
If you don't want to commit, or want to commit later (commit message will still be prepared for you, which you can also edit):
如果你不想提交,或者想稍后提交(提交消息仍然会为你准备,你也可以编辑):
git revert -n <commit>