我遇到了合并冲突。我怎样才能中止合并?

时间:2022-06-21 09:09:12

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?

我知道该文件的其他版本是好的,我的是坏的所以我的所有更改都应该放弃。我怎样才能做到这一点?

10 个解决方案

#1


1822  

Since your pull was unsuccessful then HEAD (not HEAD^) is the last "valid" commit on your branch:

由于您的拉动不成功,因此HEAD(而不是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

#2


1662  

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手册页

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

当MERGE_HEAD存在时,git merge --abort相当于git reset --merge。

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 pop。

#3


406  

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在某些情况下将无法重建这些更改。因此,建议在运行git merge之前始终提交或存储更改。

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

当MERGE_HEAD存在时,git merge --abort相当于git reset --merge。

http://www.git-scm.com/docs/git-merge

#4


74  

I think it's git reset you need.

我认为这是你需要的git重置。

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中,revert将丢弃你的(未提交的)更改,将文件从存储库返回到当前版本,而git revert“撤消”提交。

git reset should do the equivalent of svn revert, that is, discard your unwanted changes.

git reset应该相当于svn revert,也就是说,丢弃你不需要的更改。

#5


73  

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使索引中的文件的公共基础,本地和远程版本可用。 (这是git mergetool在3-way diff工具中使用的地方。)你可以使用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

#6


30  

Since 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,因为存在MERGE_HEAD。这可以在git help for merge命令中读取。

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 --merge but 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 --merge much more powerful for scenarios similar to the described one, and failed merges in general.

就个人而言,我发现git reset - 对于类似于上述场景的场景更加强大 - 并且通常会失败合并。

#7


18  

And if you end up with merge conflict and doesn't have any things to commit but still 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

file [cut paste to some other location in case of recovery] and then enter any of below command depending on which version you want.

file [在恢复的情况下将粘贴剪切到其他位置],然后根据所需的版本输入以下任何一个命令。

git reset --hard HEAD
git reset --hard origin

Hope that helps!!!

希望有所帮助!

#8


16  

Since Git 1.6.1.3 git checkout has been able to checkout from either side of a merge:

由于Git 1.6.1.3 git checkout已经能够从合并的任何一方签出:

git checkout --theirs _widget.html.erb

#9


14  

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中合并一样,因为它会抛弃以下提交中的分支关系。

#10


1  

I found the following worked for me (revert a single file to pre-merge state):

我发现以下内容适用于我(将单个文件还原为预合并状态):

git reset *currentBranchIntoWhichYouMerged* -- *fileToBeReset*

#1


1822  

Since your pull was unsuccessful then HEAD (not HEAD^) is the last "valid" commit on your branch:

由于您的拉动不成功,因此HEAD(而不是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

#2


1662  

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手册页

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

当MERGE_HEAD存在时,git merge --abort相当于git reset --merge。

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 pop。

#3


406  

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在某些情况下将无法重建这些更改。因此,建议在运行git merge之前始终提交或存储更改。

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

当MERGE_HEAD存在时,git merge --abort相当于git reset --merge。

http://www.git-scm.com/docs/git-merge

#4


74  

I think it's git reset you need.

我认为这是你需要的git重置。

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中,revert将丢弃你的(未提交的)更改,将文件从存储库返回到当前版本,而git revert“撤消”提交。

git reset should do the equivalent of svn revert, that is, discard your unwanted changes.

git reset应该相当于svn revert,也就是说,丢弃你不需要的更改。

#5


73  

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使索引中的文件的公共基础,本地和远程版本可用。 (这是git mergetool在3-way diff工具中使用的地方。)你可以使用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

#6


30  

Since 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,因为存在MERGE_HEAD。这可以在git help for merge命令中读取。

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 --merge but 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 --merge much more powerful for scenarios similar to the described one, and failed merges in general.

就个人而言,我发现git reset - 对于类似于上述场景的场景更加强大 - 并且通常会失败合并。

#7


18  

And if you end up with merge conflict and doesn't have any things to commit but still 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

file [cut paste to some other location in case of recovery] and then enter any of below command depending on which version you want.

file [在恢复的情况下将粘贴剪切到其他位置],然后根据所需的版本输入以下任何一个命令。

git reset --hard HEAD
git reset --hard origin

Hope that helps!!!

希望有所帮助!

#8


16  

Since Git 1.6.1.3 git checkout has been able to checkout from either side of a merge:

由于Git 1.6.1.3 git checkout已经能够从合并的任何一方签出:

git checkout --theirs _widget.html.erb

#9


14  

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中合并一样,因为它会抛弃以下提交中的分支关系。

#10


1  

I found the following worked for me (revert a single file to pre-merge state):

我发现以下内容适用于我(将单个文件还原为预合并状态):

git reset *currentBranchIntoWhichYouMerged* -- *fileToBeReset*