如何撤消存在冲突的 git 合并

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5741407/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:42:32  来源:igfitidea点击:

How to undo a git merge with conflicts

gitgit-mergegit-merge-conflict

提问by Anshul

I am on branch mybranch1. mybranch2is forked from mybranch1and changes were made in mybranch2.

我在分支上mybranch1mybranch2分叉自mybranch1并在 中进行了更改mybranch2

Then, while on mybranch1, I have done git merge --no-commit mybranch2It shows there were conflicts while merging.

然后,在mybranch1,我已经完成了git merge --no-commit mybranch2它表明合并时存在冲突。

Now I want do discard everything (the mergecommand) so that mybranch1is back to what it was before. I have no idea how do I go about this.

现在我想丢弃所有内容(merge命令),以便mybranch1恢复到以前的状态。我不知道我该怎么做。

回答by Daniel Cassidy

Latest Git:

最新的 Git:

git merge --abort

This attempts to reset your working copy to whatever state it was in before the merge. That means that it should restore any uncommitted changes from before the merge, although it cannot always do so reliably. Generally you shouldn't merge with uncommitted changes anyway.

这会尝试将您的工作副本重置为合并之前的任何状态。这意味着它应该从合并之前恢复任何未提交的更改,尽管它不能总是可靠地这样做。通常,您无论如何都不应该合并未提交的更改。

Prior to version 1.7.4:

1.7.4 版之前:

git reset --merge

This is older syntax but does the same as the above.

这是较旧的语法,但与上述语法相同。

Prior to version 1.6.2:

1.6.2 之前的版本:

git reset --hard

which removes all uncommitted changes, including the uncommitted merge. Sometimes this behaviour is useful even in newer versions of Git that support the above commands.

这将删除所有未提交的更改,包括未提交的合并。有时,即使在支持上述命令的较新版本的 Git 中,这种行为也很有用。

回答by Martin G

Actually, it is worth noticing that git merge --abortis only equivalent to git reset --mergegiven that MERGE_HEADis present. This can be read in the git help for merge command.

实际上,值得注意的git merge --abort是,仅相当于给git reset --mergeMERGE_HEAD存在。这可以在合并命令的 git 帮助中阅读。

git merge --abort # is equivalent to git reset --merge when MERGE_HEAD is present.

After a failed merge, when there is no MERGE_HEAD, the failed merge can be undone with git reset --mergebut not necessarily with git merge --abort, so they are not only old and new syntax for the same thing.

合并失败后,如果没有MERGE_HEAD,失败的合并可以用 撤消,git reset --merge但不一定用git merge --abort因此它们不仅是同一事物的新旧语法

Personally I find git reset --mergemuch more useful in everyday work.

我个人觉得git reset --merge在日常工作中更有用。

回答by Adam Dymitruk

Assuming you are using the latest git,

假设您使用的是最新的 git,

git merge --abort

回答by Manish Kumar Singh

There are two things you can do first undo merge by command

有两件事你可以先通过命令撤消合并

git merge --abort

or

或者

you can go to your previous commit state temporarily by command

你可以通过命令临时进入你之前的提交状态

git checkout 0d1d7fc32 

回答by Kamil Kie?czewski

Sourcetree

源码树

If you not commit your merge, then just double click on another branch (=checkout) and when sourcetree ask you about discarding all changes then agree

如果您不提交合并,则只需双击另一个分支(= checkout),当 sourcetree 询问您是否放弃所有更改时,请同意