2010-05-06 197 views

回答

17

您可以使用git checkout --conflict=merge -- file获取冲突标记文件的内容,但是如果您已使用git add file(或者如果GUI为您做了这些)清除了索引,则它不起作用。

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

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

+1

嘿,我只是写了这个回复。 ;-) – ebneter 2010-05-07 01:24:27

+1

什么是hacky和不可靠的呢? – 2010-05-10 12:52:00

+1

'git update-index --unresolve'是在“古代”时代创建的,以允许在确认冲突解决之后(意外)'git add'恢复'git diff --ours'等。它在阶段#2填充HEAD版本(不包括解决了自动解决的冲突的版本),在阶段#3填充MERGE_HEAD版本,并且不在阶段#1中放入任何东西,即祖先版本。 – 2010-05-10 21:37:20

2

据我所知,当文件中仍然包含冲突标记时,您将无法提交。 ......这是不完全正确:
的OP提到,你可以(我复制到这里his pastbin),但不会有足够的合并工具再次被触发:

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的合并工具被查询,因为有在索引同一文件的3个实例:

对于在冲突的git毫安一个未合并文件kes提供索引中文件的通用基础,本地和远程版本。 (这是他们从一个三路比较工具使用的git mergetool阅读。)你可以使用git节目进行查看:

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

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

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

git add(与任何内容,包括冲突标记)会自动删除其中的2个,确保mergetool不会再次调用

+0

确实可以。仅仅通过git add 将删除标记,然后您可以自由提交。 http://pastebin.com/KKLtCZ35 – 2010-05-06 17:53:09

+0

@Christian:有趣的(我已经修改了答案以反映它),但git mergetool会检测到它并重新触发合并? – VonC 2010-05-06 18:14:40

+0

如果git在索引中有多个条目,而不仅仅是通常的条目,git会确定文件发生冲突。 git在工作树版本中放置冲突标记以帮助用户解决冲突,但这些并不是git将文件计入未装入的。调用git add告诉git将文件的工作树版本添加到索引_removing所有其他entries_。在git添加之后,因为现在只有一个索引条目,文件不再是'未解析的',因此您可以提交它。 – 2010-05-06 21:56:50

1

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

 
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$ 

混帐合并工具可以接受一个文件名,但是,这并不工作,要么:这里

 
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。

+0

在Charles的评论之后刚刚完成了我的回答。 – VonC 2010-05-07 04:16:48

+0

s/lynx:。* \ $/\ n#/ g – user1133275 2017-05-29 16:24:56

5

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

+1

谨慎阐述? – Tarrasch 2013-07-12 02:57:24

+2

是的。这将导致Git每次询问文件是否成功合并,而不是依靠合并工具本身来如实报告成功或失败。 – 2013-07-12 06:43:52

+1

那么这是否意味着它不会'git add'文件,除非你回答是?我也有'mergetool.prompt = false'会影响这个吗? – Superole 2013-11-11 09:17:08

10

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

git checkout --conflict=merge file 

如果该指数是干净的,因为悬而未决的文件已被[误]添加,检查出来之前刚刚复位:

git reset file 
git checkout --conflict=merge file 

这将允许你继续解决冲突通常(例如,git mergetool )。

备注:通过@fourpastmidnight的请求,将@ jakub-narębski的回答推荐到自己的答案中。 :)

相关问题