【问题标题】:git: How to redo a merge conflict resolution (before committing the merge)?git:如何重做合并冲突解决方案(在提交合并之前)?
【发布时间】:2015-04-13 14:35:15
【问题描述】:

我做了一个

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")

只是一个想法:简单地对 nVP.ini 进行 git checkout 是否会恢复合并之前的情况?

【问题讨论】:

  • 如果nVP.ini 是冲突文件,您可以只使用git checkout HEAD -- nVP.inigit reset 只是重置索引条目,你也希望工作树干净,所以检查 HEAD 版本。在合并期间你也可以git checkout --ours nVP.ini 达到同样的效果,

标签: git merge


【解决方案1】:

要在提交前撤消错误的冲突解决方案,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

【讨论】:

  • 这只会导致error: pathspec 'xxx' did not match any file(s) known to git. 错误。
  • Krzaku 如果 git 可以找到该文件,您会收到该错误,您需要输入 git status 显示的路径。 @jthill 感谢您的回答,您为我节省了大约 4 小时或合并地狱。
【解决方案2】:

jthill's answer 正是我想要的,但我想我要指出,如果您是喜欢git restore 而不是git checkout 的人之一,等效的命令是:

git restore --merge -- <filepath>

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

【讨论】:

    【解决方案3】:

    我有同样的问题,

    在 dev 中合并了一个分支,这在冲突解决期间擦除了另一个分支更改。

    我为此使用了cherry-pick:

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

    我能够重做整个冲突解决过程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      • 2021-07-28
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      相关资源
      最近更新 更多