git 我遇到了合并冲突。如何中止合并?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/101752/
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
I ran into a merge conflict. How can I abort the merge?
提问by Gwyn Morfey
I used git pull
and had a merge conflict:
我使用git pull
并发生了合并冲突:
unmerged: _widget.html.erb
You are in the middle of a conflicted merge.
I know that the other version of the file is good and that mine is bad so all my changes should be abandoned. How can I do this?
我知道该文件的另一个版本是好的,而我的则是坏的,因此应该放弃我的所有更改。我怎样才能做到这一点?
回答by Pat Notz
Since your pull
was unsuccessful then HEAD
(not HEAD^
) is the last "valid" commit on your branch:
由于您pull
不成功,那么HEAD
(not HEAD^
) 是您分支上的最后一个“有效”提交:
git reset --hard HEAD
The other piece you want is to let their changes over-ride your changes.
您想要的另一部分是让他们的更改覆盖您的更改。
Older versions of git allowed you to use the "theirs" merge strategy:
旧版本的 git 允许您使用“他们的”合并策略:
git pull --strategy=theirs remote_branch
But this has since been removed, as explained in this message by Junio Hamano(the Git maintainer). As noted in the link, instead you would do this:
但这已被删除,正如Junio Hamano(Git 维护者)在此消息中所解释的那样。如链接中所述,您可以这样做:
git fetch origin
git reset --hard origin
回答by Carl
If your git version is >= 1.6.1, you can use git reset --merge
.
如果您的 git 版本 >= 1.6.1,您可以使用git reset --merge
.
Also, as @Michael Johnson mentions, if your git version is >= 1.7.4, you can also use git merge --abort
.
另外,正如@Michael Johnson 提到的,如果您的 git 版本 >= 1.7.4,您也可以使用git merge --abort
.
As always, make sure you have no uncommitted changes before you start a merge.
与往常一样,在开始合并之前,请确保没有未提交的更改。
From the git merge man page
git merge --abort
is equivalent to git reset --merge
when MERGE_HEAD
is present.
git merge --abort
相当于git reset --merge
when MERGE_HEAD
is present。
MERGE_HEAD
is present when a merge is in progress.
MERGE_HEAD
正在进行合并时存在。
Also, regarding uncommitted changes when starting a merge:
此外,关于开始合并时未提交的更改:
If you have changes you don't want to commit before starting a merge, just git stash
them before the merge and git stash pop
after finishing the merge or aborting it.
如果您在开始合并之前不想提交更改,请在合并git stash
之前和git stash pop
完成合并或中止合并之后提交。
回答by ignis
git merge --abort
Abort the current conflict resolution process, and try to reconstruct the pre-merge state.
If there were uncommitted worktree changes present when the merge started,
git merge --abort
will in some cases be unable to reconstruct these changes. It is therefore recommended to always commit or stash your changes before running git merge.
git merge --abort
is equivalent togit reset --merge
whenMERGE_HEAD
is present.
中止当前的冲突解决过程,并尝试重建合并前的状态。
如果在合并开始时存在未提交的工作树更改,
git merge --abort
在某些情况下将无法重建这些更改。因此,建议在运行 git merge 之前始终提交或隐藏您的更改。
git merge --abort
相当于git reset --merge
whenMERGE_HEAD
is present。
回答by Jagruttam Panchal
Its so simple.
它是如此简单。
git merge --abort
Git itself shows you the solution when you are in this type of trouble and run the git status command.
当您遇到此类问题并运行 git status 命令时,Git 本身会向您展示解决方案。
git status
Hope this will help people.
希望这会帮助人们。
回答by David Precious
I think it's git reset
you need.
我认为这是git reset
你需要的。
Beware that git revert
means something very different to, say, svn revert
- in Subversion the revert will discard your (uncommitted) changes, returning the file to the current version from the repository, whereas git revert
"undoes" a commit.
请注意,这git revert
意味着非常不同的事情,例如svn revert
- 在 Subversion 中,还原将丢弃您的(未提交的)更改,将文件从存储库返回到当前版本,而git revert
“撤消”提交。
git reset
should do the equivalent of svn revert
, that is, discard your unwanted changes.
git reset
应该做相当于svn revert
,即丢弃您不需要的更改。
回答by CB Bailey
In this particular use case, you don't really want to abort the merge, just resolve the conflict in a particular way.
在此特定用例中,您并不真的想中止合并,只需以特定方式解决冲突即可。
There is no particular need to reset and perform a merge with a different strategy, either. The conflicts have been correctly highlighted by git and the requirement to accept the other sides changes is only for this one file.
也没有特别需要重置和使用不同的策略执行合并。git 正确地突出了冲突,接受其他方面更改的要求仅适用于这个文件。
For an unmerged file in a conflict git makes available the common base, local and remote versions of the file in the index. (This is where they are read from for use in a 3-way diff tool by git mergetool
.) You can use git show
to view them.
对于冲突中未合并的文件,git 使索引中文件的公共基础、本地和远程版本可用。(这是它们的读取位置,用于 3-way diff 工具git mergetool
。)您可以使用git show
来查看它们。
# common base:
git show :1:_widget.html.erb
# 'ours'
git show :2:_widget.html.erb
# 'theirs'
git show :3:_widget.html.erb
The simplest way to resolve the conflict to use the remote version verbatim is:
解决冲突以逐字使用远程版本的最简单方法是:
git show :3:_widget.html.erb >_widget.html.erb
git add _widget.html.erb
Or, with git >= 1.6.1:
或者,使用 git >= 1.6.1:
git checkout --theirs _widget.html.erb
回答by naamadheya
For scenario like, I did git fetch
and git pull
, then realized that upstream branch was not master branch, which resulted in unwanted conflicts.
对于类似的场景,我做了git fetch
和git pull
,然后意识到上游分支不是主分支,这导致了不必要的冲突。
git reset --merge
This reverted back without resetting my local changes.
这在没有重置我的本地更改的情况下恢复了。
回答by Martin G
Comments suggest that git reset --merge
is an alias for git merge --abort
. It is worth noticing that git merge --abort
is only equivalent to git reset --merge
given that a MERGE_HEAD
is present. This can be read in the git help for merge command.
评论表明这git reset --merge
是 的别名git merge --abort
。值得注意的是,git merge --abort
仅等价于git reset --merge
给定 aMERGE_HEAD
存在。这可以在合并命令的 git 帮助中阅读。
git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present.
当 MERGE_HEAD 存在时, git merge --abort 等效于 git reset --merge 。
After a failed merge, when there is no MERGE_HEAD
, the failed merge can be undone with git reset --merge
, but not necessarily with git merge --abort
. 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 --merge
much more powerful for scenarios similar to the described one, and failed merges in general.
就个人而言,我发现git reset --merge
对于与所描述的场景类似的场景更强大,并且通常合并失败。
回答by Nirav Mehta
If you end up with merge conflict and doesn't have anything to commit, but still a merge error is being displayed. After applying all the below mentioned commands,
如果您最终遇到合并冲突并且没有任何要提交的内容,但仍会显示合并错误。应用下面提到的所有命令后,
git reset --hard HEAD
git pull --strategy=theirs remote_branch
git fetch origin
git reset --hard origin
Please remove
请删除
.git\index.lock
.git\index.lock
File [cut paste to some other location in case of recovery] and then enter any of below command depending on which version you want.
文件[剪切粘贴到其他位置以备恢复],然后根据您想要的版本输入以下任何命令。
git reset --hard HEAD
git reset --hard origin
Hope that helps!!!
希望有帮助!!!
回答by Alain O'Dea
An alternative, which preserves the state of the working copy is:
另一种保留工作副本状态的替代方法是:
git stash
git merge --abort
git stash pop
I generally advise against this, because it is effectively like merging in Subversion as it throws away the branch relationships in the following commit.
我通常不建议这样做,因为它实际上就像在 Subversion 中合并一样,因为它会在接下来的提交中丢弃分支关系。