Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 830
Svetan Dimoff Avatar asked Jun 16 '15 14:06

Svetan Dimoff


People also ask

What condition will always cause a merge conflict?

Understanding merge conflicts Most of the time, Git will figure out how to automatically integrate new changes. Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it.

What happens if you get a conflict during a merge?

Merge conflicts happen when you merge branches that have competing commits, and Git needs your help to decide which changes to incorporate in the final merge. Git can often resolve differences between branches and merge them automatically.


1 Answers

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

like image 200
David Deutsch Avatar answered Nov 09 '22 13:11

David Deutsch