Merge conflict, when branches aren't modifying the same line

1.6k views Asked by At

I'm trying to wrap my head around git conflicts, why does merging these two result in a conflict?

file.txt on branch master:

This is line number one

file.txt on branch feature:

This is line number one
This is line number two
1

There are 1 answers

0
David Deutsch On

Normally, this would not cause a conflict (assuming that there was not a second line in the base file). But in this case, you added file.txt separately to master and feature (which is why the file is not in the common ancestor). When a file is independently added to two branches that are then merged and they differ in any way, Git considers it a conflict.

The reason for this is that Git needs a base copy to determine what the state of the file should be after the merge. For example, let's say there was a base file, and it had only the one line. In that case, Git would know that a line was added in feature, and thus the final file would have the two lines. If the base file had two lines, however, Git would know that a line was removed in master, and thus the final file would have a single line. In your case, there is no base file, so Git does not know which version of the file should "win".