有没有办法让Git将文件标记为冲突?

时间:2010-05-06 10:54:51

标签: git merge mergetool

可以提交包含冲突数据的文件。有没有办法再次将这些文件标记为冲突,以便运行git mergetool会生成必要的文件并运行合并工具?

6 个答案:

答案 0 :(得分:20)

您可以使用git checkout --conflict=merge -- file获取包含冲突标记的文件内容,但如果您使用git add file清理了索引(或者如果GUI为您执行了此操作),则无法使用。

git update-index --unresolve,但它很hacky,并且不能非常可靠地工作。我认为它恢复的状态对于git-mergetool来说是不够的。

您可能需要重做合并,或使用git update-index --cacheinfo手动设置阶段版本... git-stash可以帮助您保存正确解决的冲突。

答案 1 :(得分:18)

如果索引已经处于冲突状态,只需使用--conflict=merge标志签出文件:

git checkout --conflict=merge file

如果索引是干净的,因为[错误地]添加了未解析的文件,只需在检出之前重置它:

git reset file
git checkout --conflict=merge file

这将允许您正常恢复冲突解决(例如,git mergetool)。

注意:根据@fourpastmidnight的要求,将对@jakub-narębski的答案的评论提升为自己的答案。 :)

答案 2 :(得分:5)

最优雅的解决方案是从一开始就防止这个问题:
git config --global mergetool.[tool].cmd [command-line call]
git config --global mergetool.[tool].trustExitCode false

答案 3 :(得分:2)

据我所知,当文件中仍然包含冲突标记时,你将无法提交。 ...这不完全正确:
OP提到你可以(我在这里复制his pastbin),但这不足以让mergetool再次触发:

Auto-merged README
CONFLICT (content): Merge conflict in README
Automatic merge failed; fix conflicts and then commit the result.
lynx:~/test_clone$ ls
README
lynx:~/test_clone$ git add README
lynx:~/test_clone$ git commit -a
Created commit 46ee062: It works!
lynx:~/test_clone$ ls
README
lynx:~/test_clone$ cat README
<<<<<<< HEAD:README
testingtesting
=======
hmm
>>>>>>> 881d60f5f738bc5716f5c9a9384e262b535717fd:README
lynx:~/test_clone$

作为Charles Bailey注释,并在此SO answer中说明,会查询mergetool,因为索引中有3个相同文件的实例

  

对于冲突中的未合并文件,git使索引中的文件的公共基础,本地和远程版本可用。 (这是它们被git mergetool读取用于三向差异工具的地方。)你可以使用git show来查看它们:

# common base:
git show :1:afile.txt

# 'ours'
git show :2:afile.txt

# 'theirs'
git show :3:afile.txt

git add(包含任何内容,包括冲突标记)会自动删除其中的2个,确保{<1}} 不会再次被调用。

答案 4 :(得分:0)

@VonC:我最初没有创建帐户(我现在有),所以我无法发表评论。 调用git mergetool并没有检测到它,它似乎是:

Auto-merged README
CONFLICT (content): Merge conflict in README
Automatic merge failed; fix conflicts and then commit the result.
lynx:~/test_clone$ ls
README
lynx:~/test_clone$ git add README
lynx:~/test_clone$ git commit -a
Created commit 46ee062: It works!
lynx:~/test_clone$ ls
README
lynx:~/test_clone$ cat README
>>>>>> 881d60f5f738bc5716f5c9a9384e262b535717fd:README
lynx:~/test_clone$ git mergetool
merge tool candidates:  opendiff emerge vimdiff
No files need merging
lynx:~/test_clone$

git mergetool可以接受文件名,但这也不起作用:

Auto-merged README
CONFLICT (content): Merge conflict in README
Automatic merge failed; fix conflicts and then commit the result.
caracal:~/test_clone2$ git mergetool
merge tool candidates:  opendiff emerge vimdiff
Merging the files: README

Normal merge conflict for 'README':
  {local}: modified
  {remote}: modified
Hit return to start merge resolution tool (emerge): 
caracal:~/test_clone2$ ls
#*merge*#145962bz#  README  README~  README.orig
caracal:~/test_clone2$ git mergetool
merge tool candidates:  opendiff emerge vimdiff
No files need merging
caracal:~/test_clone2$ git mergetool README
merge tool candidates:  opendiff emerge vimdiff

README: file does not need merging
caracal:~/test_clone2$ ls
#*merge*#145962bz#  README  README~  README.orig
caracal:~/test_clone2$ 

另请注意,退出git mergetool后我没有提交。

答案 5 :(得分:0)

请使用git update-index --unresolve

从git 1.7开始,它使用索引中的resolve-undo信息来恢复所有3个阶段(1:基数,2:我们的,3:他们的):https://github.com/git/git/commit/8aa38563b22c84b06ea1fff9638cc1f44fda726f

相关问题