Should diff3 be default conflictstyle on git?

后端 未结 3 953
一整个雨季
一整个雨季 2020-12-08 04:01

Recently I enabled diff3 and it\'s much easier to resolve conflict now.

Previously in some cases I had to check the log to see why people did this and that to do the

3条回答
  •  不思量自难忘°
    2020-12-08 04:47

    diff3 should be the default. It is not only useful for resolving conflicts, it makes resolving conflicts possible. It is literally impossible to correctly resolve conflicts using (only) the default merge conflict style. I suggest to everyone to set diff3 in their global options.

    git config --global merge.conflictStyle diff3
    

    Why is it literally impossible? Consider a branch which adds a function foo1 at a particular source file location

    def foo1():
        print("foo1")
    

    and another branch which adds a function foo2 at the same location

    def foo2():
        print("foo2")
    

    If I rebase one on the other I get a conflict. The default merge conflict style will show

    ++<<<<<<< HEAD
     +def foo1():
     +    print("foo1")
    ++=======
    + def foo2():
    +     print("foo2")
    ++>>>>>>> Add foo2
    

    What are the conflict markers telling me? They're telling me that I need to add both foo1 and foo2 to the file, right? Unfortunately not! Consider a file in which foo1 and foo2 already exist, and two branches, one of which removes foo1 and one of which removes foo2. If I rebase one on the other what is the result? The default merge conflict style will show

    ++<<<<<<< HEAD
     +def foo1():
     +    print("foo1")
    ++=======
    + def foo2():
    +     print("foo2")
    ++>>>>>>> Remove foo1
    

    Under the default conflict style the case of removing two functions is completely indistinguishable from the case of adding two functions (besides the text of the commit message which can only ever be a hint)! Therefore it is insufficient for purpose of resolving conflicts. This probably explains why resolving conflicts is seen as a dark art. diff3 not only makes it possible, it often makes it easy.

提交回复
热议问题