Why does git produce a merge conflict when lines next to each other are changed?

前端 未结 3 820
盖世英雄少女心
盖世英雄少女心 2020-12-03 17:48

Let\'s say I have a file with this content in master:

Line 1
Line 2
Line 3
Line 4

Now say I create and checkout a new branch c

相关标签:
3条回答
  • 2020-12-03 18:11

    You can specify a custom merge driver that will not conflict in this case in a .gitattributes file that's in the repo.

    0 讨论(0)
  • 2020-12-03 18:13

    The reason that Git behaves like this is explained well in the answers to this question:

    https://softwareengineering.stackexchange.com/questions/194788/why-doesnt-git-merge-adjacent-lines-without-conflict/378258#378258

    Essentially, because you need the neighboring lines to provide context to the change (you can't just use line numbers, because something may have been added or deleted above), if the lines around it have changed you usually don't want Git to just naively continue with the merge. User Arsen7 gives a good example in that thread of how this could go badly wrong.

    However, I agree with you that sometimes this is quite annoying, so I wrote a custom merge driver that can resolve such conflicts during merging/rebasing. It's designed to be interactive, because I always want to check that it's going to do the right thing before going ahead, but you could easily modify it not to be if you're confident it's going to work.

    If you're interested, the script is available on GitHub under a GPLv3+ license:

    https://github.com/paulaltin/git-subline-merge

    0 讨论(0)
  • 2020-12-03 18:26

    I stumbled upon the same problem, coming from SVN I found this very weird as well.

    I do not have an answer to the why, but maybe this helps:

    I use another merge tool (depending on what OS you are working on), i use meld diff for solving merge conflicts (I work on linux / ubuntu).

    And you can set git to use this external merge application as well...

    see http://meldmerge.org/

    and google for 'use meld for git'

    e.g. http://meldmerge.org/help/resolving-conflicts.html

    0 讨论(0)
提交回复
热议问题