T. Dimoff
T. Dimoff

Reputation: 565

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

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

Upvotes: 6

Views: 1659

Answers (1)

David Deutsch
David Deutsch

Reputation: 19025

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".

Upvotes: 10

Related Questions