我遇到了合并冲突。 如何中止合并?
我使用了 git pull 并遇到了合并冲突:
unmerged: some_file.txt
You are in the middle of a conflicted merge.
如何放弃对文件的更改并仅保留拉取的更改?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
对于 git >= 1.6.1:
对于旧版本的 git,这将完成这项工作:
或
For git >= 1.6.1:
For older versions of git, this will do the job:
or
您可以中止合并步骤:
否则您可以保留您的更改(您所在的分支),
否则您可以保留其他分支更改
You can either abort the merge step:
else you can keep your changes (on which branch you are)
otherwise you can keep other branch changes
在这个特定的用例中,您并不是真的想中止合并,只是以特定的方式解决冲突。
也没有特别需要重置并使用不同的策略执行合并。 git 已正确突出显示冲突,并且接受另一方更改的要求仅适用于该文件。
对于冲突中未合并的文件,git 在索引中提供该文件的公共基础版本、本地版本和远程版本。 (这是
git mergetool
读取它们以便在 3 向 diff 工具中使用的位置。)您可以使用git show
来查看它们。解决逐字使用远程版本冲突的最简单方法是:
或者,使用 git >= 1.6.1:
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.
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 usegit show
to view them.The simplest way to resolve the conflict to use the remote version verbatim is:
Or, with git >= 1.6.1:
评论表明 git reset --merge 是 git merge --abort 的别名。 值得注意的是,在存在
MERGE_HEAD
的情况下,git merge --abort
仅相当于git reset --merge
。 这可以在 git 合并命令帮助中阅读。合并失败后,当没有
MERGE_HEAD
时,可以使用git reset --merge
撤消失败的合并,但不一定使用git merge --abort
。 它们不仅仅是同一事物的新旧语法。就我个人而言,我发现 git reset --merge 对于与所描述的类似的场景以及一般失败的合并来说更强大。
Comments suggest that
git reset --merge
is an alias forgit merge --abort
. It is worth noticing thatgit merge --abort
is only equivalent togit reset --merge
given that aMERGE_HEAD
is present. This can be read in the git help for merge command.After a failed merge, when there is no
MERGE_HEAD
, the failed merge can be undone withgit reset --merge
, but not necessarily withgit merge --abort
. They are not only old and new syntax for the same thing.Personally, I find
git reset --merge
much more powerful for scenarios similar to the described one, and failed merges in general.如果您最终遇到合并冲突并且没有任何可提交的内容,但仍然显示合并错误。 应用以下所有命令后,
请删除
文件[将粘贴剪切到其他位置以备恢复],然后根据您想要的版本输入以下任意命令。
希望有帮助!
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,
Please remove
File [cut paste to some other location in case of recovery] and then enter any of below command depending on which version you want.
Hope that helps!!!
保留工作副本状态的另一种选择是:
我通常建议不要这样做,因为它实际上就像在 Subversion 中合并一样,因为它会在接下来的提交中丢弃分支关系。
An alternative, which preserves the state of the working copy is:
I generally advise against this, because it is effectively like merging in Subversion as it throws away the branch relationships in the following commit.
可能不是OP想要的,但对我来说,我尝试将稳定分支合并到功能分支,但存在太多冲突。
我没有设法重置更改,因为 HEAD 已被多次提交更改,因此简单的解决方案是强制签出到稳定分支。
然后您可以签出到另一个分支,它将与合并之前一样。
git checkout -f master
git checkout side-branch
Might not be what the OP wanted, but for me I tried to merge a stable branch to a feature branch and there were too many conflicts.
I didn't manage to reset the changes since the HEAD was changed by many commits, So the easy solution was to force checkout to a stable branch.
you can then checkout to the other branch and it will be as it was before the merge.
git checkout -f master
git checkout side-branch
从 Git 1.6.1.3 开始
git checkout
已经可以签出从合并的任一侧:Since Git 1.6.1.3
git checkout
has been able to checkout from either side of a merge:为了避免陷入此类麻烦,可以扩展
git merge --abort
方法并在之前创建一个单独的测试分支合并。案例:您有一个主题分支,它没有被合并,因为您分心/出现了一些事情/您知道但它已经(或已经)准备好了。
现在是否可以将其合并到master中?
在测试分支中工作以估计/找到解决方案,然后放弃测试分支并在主题分支中应用该解决方案。
致力于解决冲突。
最后再次检查主题分支,应用测试分支中的修复并继续 PR。
最后删除测试和主测试。
涉及? 是的,但在我准备好之前,它不会扰乱我的主题或主分支。
To avoid getting into this sort of trouble one can expand on the
git merge --abort
approach and create a separate test branch before merging.Case: You have a topic branch, it wasn't merged because you got distracted/something came up/you know but it is (or was) ready.
Now is it possible to merge this into master?
Work in a test branch to estimate / find a solution, then abandon the test branch and apply the solution in the topic branch.
Work on resolving the conflict.
Finally checkout the topic branch again, apply the fix from the test branch and continue with the PR.
Lastly delete the test and master-test.
Involved? Yes, but it won't mess with my topic or master branch until I'm good and ready.
我发现以下内容对我有用(将单个文件恢复到合并前状态):
I found the following worked for me (revert a single file to pre-merge state):
由于您的
pull
不成功,因此HEAD
(不是HEAD^
)是您分支上的最后一个“有效”提交:您想要的另一部分是让他们的更改覆盖您的更改。
旧版本的 git 允许您使用“他们的”合并策略:
但这已被删除,如 此消息由 Junio Hamano(Git 维护者)发出。 正如链接中所述,您可以这样做:
Since your
pull
was unsuccessful thenHEAD
(notHEAD^
) is the last "valid" commit on your branch: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:
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:
如果您的 git 版本 >= 1.6.1,您可以使用 git reset --merge 。
另外,正如 @Michael Johnson 提到的,如果您的 git 版本 >= 1.7.4,您还可以使用 git merge --abort 。
与往常一样,请确保在开始合并之前没有未提交的更改。
来自 git merge 手册页
当
相当于MERGE_HEAD
存在时, git merge --abortgit reset --merge
。合并正在进行时,会出现
MERGE_HEAD
。另外,关于开始合并时未提交的更改:
如果您有不想在开始合并之前提交的更改,只需在合并之前
git stash
它们,然后git stash pop
完成合并或中止合并后。If your git version is >= 1.6.1, you can use
git reset --merge
.Also, as @Michael Johnson mentions, if your git version is >= 1.7.4, you can also use
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 togit reset --merge
whenMERGE_HEAD
is present.MERGE_HEAD
is present when a merge is in progress.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 andgit stash pop
after finishing the merge or aborting it.http://www.git-scm.com/docs/git-merge
http://www.git-scm.com/docs/git-merge
我认为这是您需要的 git reset 。
请注意,
git revert
的含义与svn revert
等含义非常不同 - 在 Subversion 中,还原将放弃您的(未提交的)更改,将文件从当前版本返回到当前版本。存储库,而 git revert 则“撤消”提交。git reset
应该执行与svn revert
相同的操作,即放弃不需要的更改。I think it's
git reset
you need.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, whereasgit revert
"undoes" a commit.git reset
should do the equivalent ofsvn revert
, that is, discard your unwanted changes.