2015-11-09 46 views
2

我希望能夠自動解決「兩個分支在同一行添加塊」衝突,首先把「我們」塊,然後是「他們的」塊。我該如何讓git通過把「我們的第一個,然後他們的」我們的解決合併衝突

因此,而不是像一個矛盾:

16:09:44 [email protected] ~/git/merge_conflict_example (master|MERGING) $ cat test.txt 
good morning 
good evening 
<<<<<<< HEAD 
g'day 
======= 
aloha 
>>>>>>> branch1 
hello 
hi 

我只希望得到:

16:09:44 [email protected] ~/git/merge_conflict_example (master) $ cat test.txt 
good morning 
good evening 
g'day 
aloha 
hello 
hi 

沒有衝突。

我想有可能是這樣的git merge branch1 -X oursthentheirs

我這裏使用的樣品可在[email protected]:abznak/merge_conflict_example.git

回答

0

這很容易。只需將merge gitattribute設置爲union即可。 從https://git-scm.com/docs/gitattributes

工會

潤3路文件級合併爲文本文件,但拿的不是讓衝突標記來自 版本線。這往往會使結果文件中的 添加行以隨機順序排列,用戶應該 驗證結果。如果您不瞭解 的含義,請不要使用它。

例如,我剛剛添加了一個。包含文本*.txt merge=union gitattributes文件:

10:58:21 [email protected] ~/git/merge_conflict_example (master) $ cat .gitattributes 
*.txt merge=union 

就跑合並:

10:58:26 [email protected] ~/git/merge_conflict_example (master) $ git merge origin/branch1 
Auto-merging test.txt 
[...] 
Merge made by the 'recursive' strategy. 
test.txt | 1 + 
1 file changed, 1 insertion(+) 

哪了預期的效果:

10:58:42 [email protected] ~/git/merge_conflict_example (master) $ cat test.txt 
good morning 
good evening 
g'day 
aloha 
hello 
hi 
+0

當我遇到答案時,我正在尋找讓git自動運行@ torek解決方案的方法。 –

0

您可以合併,說分支老進當前的分支使用 「我們」 合併 戰略像下面

$ git的合併-s我們的老

+0

我覺得GIT中仍然顯示合併衝突 – hjpotter92

+1

與合併「我們」的策略完全放棄了其他樹的變化,這不是OP想要的。將'ours'修飾符合併到'遞歸'策略('-X ours')也會丟棄其他樹的更改,但僅在發生衝突時纔會發生。這保留了其他樹的一些變化,但沒有任何衝突 - 這再次不是OP所要求的。 – torek

2

沒有什麼內置的,但如果你設置merge.conflictstylediff3,編寫程序(在perl或python中,或者我會在awk中編寫一個俗氣的版本)會比較容易,它會檢查「原始」部分是空的,這將檢測「既增加」的情況,如果是這樣,只是刪除衝突標誌:

good morning 
good evening 
<<<<<<< HEAD 
g'day 
||||||| 
======= 
aloha 
>>>>>>> branch1 
hello 
hi 

這裏是我的awk腳本(這我不宣稱自己是好的,但它的工作原理上樣本輸入)。請注意,它不會很好地處理「嵌套衝突」(即,如果兩個原始衝突文件包含看起來像衝突標記的內容,則會出錯)。

BEGIN { in_conflict = retained_left = retained_mid = retained_right = 0 } 
function handle_retained(is_eof) { 
     # If the section between ||||||| and ======= is empty, 
     # retained_mid+1 == retained_right. Otherwise print 
     # all the retained conflict lines. 
     if (retained_mid + 1 == retained_right) { 
       s1 = retained_left + 1 # after <<<<<<< 
       e1 = retained_mid - 1 # before ||||||| 
       s2 = retained_right + 1 # after ======= 
       e2 = NR - 1    # before >>>>>>> 
     } else { 
       s1 = retained_left; e1 = NR 
       s2 = 1; e2 = 0 
     } 
     for (i = s1; i <= e1; i++) 
       print retained[i] 
     for (i = s2; i <= e2; i++) 
       print retained[i] 
     delete retained 
     if (is_eof) { 
       # this should never happen! 
       print "WARNING: ended input while still in conflict marker" 
       exit(1) 
     } 
} 
/^<<<<<<</ { in_conflict = 1; retained_left = NR } 
{ 
     if (!in_conflict) 
       print 
     else 
       retained[NR] = $0 
} 
/^\|\|\|\|\|\|\|/ { if (in_conflict) retained_mid = NR } 
/^=======/ { if (in_conflict) retained_right = NR } 
/^>>>>>>>/ { if (in_conflict) handle_retained(0); in_conflict = 0 } 
END { if (in_conflict) handle_retained(1) } 
+0

那麼,awk肯定看起來很有趣。 +1:D(和努力) – poke

+0

對於不熟悉awk的人,我應該添加一些註釋:'NR'是當前輸入行的「記錄」(行)編號,從1開始計數爲所有輸入線路。形式'/ pattern/{action}'的行執行給定的動作,並且一個缺少的'/ pattern /'總是執行;這些按順序執行,所以我們首先檢查7'<',然後執行打印或保留,然後檢查每行的7-'|'等等。 – torek

相關問題