沒有什麼內置的,但如果你設置merge.conflictstyle
爲diff3
,編寫程序(在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) }
當我遇到答案時,我正在尋找讓git自動運行@ torek解決方案的方法。 –