合并冲突后,git会立即显示“无需合并文件”

时间:2018-08-22 01:30:45

标签: git git-rerere

例如

> git cherry-pick <hash>
...
error: could not apply <hash>...
Resolved '<file>' using previous resolution.
> git mergetool
No files need merging
> git status
...
Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both modified: <file>

“两者均已修改”和“无需合并文件”?骗子!

2 个答案:

答案 0 :(得分:1)

我猜测问题出在某种程度上git rerere遇到了错误的合并。我认为这通常发生在当Meld不太意识到我按下Ctrl-S并将冲突标记记录到解决方案中时。为什么git仍然说“都已修改”并且不允许mergetool运行,我不知道。无论如何,修复方法如下:

git rerere forget <file>
git checkout -m <file>
git mergetool

我希望git checkout -m暗示git rerere forget或git status更明确。本来可以节省很多时间。

答案 1 :(得分:0)

您对git rerere是正确的,线索是这一行:

Resolved '<file>' using previous resolution.

我以前没有意识到这一点(部分原因是我从未真正使用过git mergetool),但是:

  

为什么git仍然说“两者都已修改”,并且不允许运行我不知道的mergetool。

这是因为git mergetool使用git rerere remaining来确定要合并的文件。

我以重新使用的分辨率重新创建了您的合并冲突:

$ echo base version > file
$ git config rerere.enabled true
$ git add file && git commit -m 'create base'
[master 8e7a009] create base
 1 file changed, 1 insertion(+)
 create mode 100644 file
$ git checkout -b branch
Switched to a new branch 'branch'
$ echo 'branch conflict' >> file
$ git add file && git commit -m 'enter branch conflict'
[branch 6661ab7] enter branch conflict
 1 file changed, 1 insertion(+)
$ git checkout -b br2 master
Switched to a new branch 'br2'
$ echo 'different, hence conflict' >> file
$ git add file && git commit -m 'create br2 conflict'
[br2 9684589] create br2 conflict
 1 file changed, 1 insertion(+)
$ git merge branch
Auto-merging file
CONFLICT (content): Merge conflict in file
Recorded preimage for 'file'
Automatic merge failed; fix conflicts and then commit the result.

请注意倒数第二行,这表明我现在要解决冲突,Git将保存结果。

$ vim file

在这里,我解决了冲突(由于全屏编辑,这种方式难以显示)。

$ git add file && git commit -m resolution
Recorded resolution for 'file'.
[br2 8b42a26] resolution

会议记录了该决议。我现在放弃了合并,然后再次运行:

$ git reset --hard HEAD^
HEAD is now at 9684589 create br2 conflict
$ git merge branch
Auto-merging file
CONFLICT (content): Merge conflict in file
Resolved 'file' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.

请注意“使用先前的分辨率”行。

现在的状态是存在未合并的路径:

$ git status
On branch br2
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   file

no changes added to commit (use "git add" and/or "git commit -a")

git ls-files --stage确认文件file有三个版本:

$ git ls-files --stage
100644 eb2222a53126bfe165c615e816f5ba268f1e628f 0       README
100644 4b9049d6a70d93a7af91b4f6b741b88ba1f1838a 1       file
100644 5974ab850c831f806f950321b08391ccea97efcf 2       file
100644 7a77704a9d365c790f5a24fe87515b8df0b7e91b 3       file

内容使用我保存的分辨率:

$ cat file
base version
resolved in br2

在启用git mergetool且启用了外壳跟踪的情况下揭示了秘密:

+ USAGE='[--tool=tool] [--tool-help] [-y|--no-prompt|--prompt] [-O<orderfile>] [file to merge] ...'
+ SUBDIRECTORY_OK=Yes
[mass snippage]
+ git rerere remaining
+ set --
+ test 0 -eq 0
+ print_noop_and_exit
+ echo 'No files need merging'
No files need merging
+ exit 0

现在该由我来做些事情,例如您对git rerere forget的欺骗:

$ git rerere forget file
Updated preimage for 'file'
Forgot resolution for file
$ git checkout -m file
$ cat file
base version
<<<<<<< ours
different, hence conflict
||||||| base
=======
branch conflict
>>>>>>> theirs

请注意,当我再次解析,添加并提交时,将记录新的分辨率:

$ git add file
$ git commit -m 're-resolved'
Recorded resolution for 'file'.
[br2 00c94b1] re-resolved

(当然,git config rerere.enabled false也可以。)