Search code examples
gitmerge-conflict-resolution

Are git conflicts less likely if I edit before or after somebody else's changes?


A colleague has a ticket that will be merged after mine (for the sake of the question, assume that's not changeable), and I need to make a change to a file they've changed.

Say the file with their changes is:

this line doesn't change

this line is edited

this is added

Is there a place in the file (assuming I can freely pick) that will reduce the possibility of merge conflicts for my colleague?


Solution

  • Changes to lines that overlap or abut result in merge conflicts (with one exception noted below). Changes to lines that do not overlap and do not "touch at the edges" do not produce merge conflicts. There are "virtual" lines above the first line of the file, and after the last one, that always touch.

    Hence, if you both start with:

    the rain
    in Spain
    falls mainly
    on the plain
    

    and one commit "touches" the first line (only) to capitalize the, and the other commit "touches" the last line (only) to add a period at the end of the sentence, there will be no conflicts. However, if someone decided to capitalize each first word of each line, there would be a conflict with the change that added the period at the end of the last line.

    The exception to the overlaps-or-abuts rule occurs when both modifications change both series of input lines to the exact same output lines. In this case, Git—or virtually any three-way merge algorithm except for "union merge"—takes just one copy of the change.

    (Note that git merge-file does support union merge, but git merge does not.)