2010-05-06 199 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的回答推薦到自己的答案中。 :)

相關問題