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
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.
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.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With