git:如何在提交合并之前重做合并冲突解决?

16

我做了一个

git merge FETCH_HEAD

之后,当git告诉我一个文件存在冲突时,我进行了

git mergetool

在我的情况下,我运行SourceGear DiffMerge作为GUI合并工具。在保存合并文件后不久,我意识到自己犯了一个错误。我只想忘记这次合并,重新开始。

由于我还没有执行过“git add”,更别说提交任何内容了,我认为我可以轻松地通过以下方式消除我的错误并重新进行合并:

git reset FILENAME
git merge FETCH_HEAD
git mergetool

这行不通。"git merge" 向我报告如下:

fatal: You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you can merge.

当然,我不想提交那个搞砸了的合并。 "git mergetool" 抱怨道。

No files need merging

我猜我在使用 "git reset" 命令时犯了一个错误。正确的做法是什么?

补充:

我已经...

git merge --abort

再说一遍:

git merge FETCH_HEAD

这产生了

error: Your local changes to the following files would be overwritten by merge: ...

git status

说:

On branch ....
Your branch is up-to-date with ......
Changes not staged for commit:
    modified:   nVP.ini
Untracked files:
    nVP.ini.orig
no changes added to commit (use "git add" and/or "git commit -a")

只是一个想法:git checkout nVP.ini是否可以使得合并之前的情况重新出现?


1
如果nVP.ini是冲突文件,您可以使用git checkout HEAD -- nVP.ini命令。git reset只会重置索引条目,您需要清除工作树,因此请检出HEAD版本。在合并期间,您也可以使用git checkout --ours nVP.ini实现相同的效果。 - jthill
3个回答

37

要在提交之前撤销错误的冲突解决方案,使用git checkout -m -- $thefile命令。

$ git merge 3fac3
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
$ vi file.txt         # fix all the conflicts. do it wrong.
$ git add file.txt    # <--- ooops
$ git checkout -m -- file.txt    # <--- the correct un-oops-ifier here

并且file.txt回到了失败的自动合并状态。

请注意,您还可以指定冲突报告样式,因此如果通常使用默认的两个差异样式,但是您遇到了棘手的情况,想要查看原始文本,您可以执行:

git checkout -m --conflict diff3 -- file.txt

1
这只会导致出现 error: pathspec 'xxx' did not match any file(s) known to git. 错误。 - Krzaku
Krzaku,如果git找不到文件,你会得到那个错误,你需要像git status显示的那样输入路径。@jthill 感谢您的答案,您为我节省了约4小时的合并之痛。 - TheLukeMcCarthy

3

jthill的答案正是我在寻找的,但我想指出,如果你是那些更喜欢使用git restore而不是git checkout的人,等效的命令如下:

git restore --merge -- <filepath>

并且

git restore --conflict=diff3 -- <filepath>

2

我遇到了同样的问题,

在解决冲突时,一个分支被合并到dev中,导致另一个分支的更改被删除。

我使用了cherry-pick来解决这个问题:

git cherry-pick {merge commit sha of missing branch code} -m 1

我能够重新完成整个冲突解决过程。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接