如何通过“先我们的,然后他们的”来解决合并冲突

时间:2015-11-09 06:30:55

标签: git git-merge

我希望能够通过首先放置“我们的”块然后将“他们的”块放在“他们的”块中来自动解决“两个分支在同一行添加块”的冲突。

所以不要像以下那样发生冲突:

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

我得到:

16:09:44 tims@firebat ~/git/merge_conflict_example (master) $ cat test.txt
good morning
good evening
g'day
aloha
hello
hi

没有冲突。

我认为可能有git merge branch1 -X oursthentheirs

之类的东西

我在这里使用的样本可以在git@bitbucket.org上找到:abznak / merge_conflict_example.git

3 个答案:

答案 0 :(得分:2)

没有内置任何内容,但是如果你将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) }

答案 1 :(得分:1)

这很容易。只需将merge gitattribute设置为union即可。 来自https://git-scm.com/docs/gitattributes

  

联合

     

为文本文件运行3向文件级别合并,但从两者中获取行      版本,而不是留下冲突标记。这往往会留下      在结果文件中以随机顺序添加行,用户应该      验证结果。如果你不明白,请不要使用它      影响。

对于该示例,我刚添加了一个包含文本*.txt merge=union的.gitattributes文件:

10:58:21 tims@thor ~/git/merge_conflict_example (master) $ cat .gitattributes
*.txt merge=union

然后运行合并:

10:58:26 tims@thor ~/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 tims@thor ~/git/merge_conflict_example (master) $  cat test.txt 
good morning
good evening
g'day
aloha
hello
hi

答案 2 :(得分:0)

您可以使用&#34;我们的&#34;合并,将分支旧分支到当前分支。合并 策略如下

  

$ git merge -s our old

相关问题