3

So I have this simple file with 3 lines:

some
random
words

First Merge Test

I created two different branches with the file in the original state at the top of this post from master. I create branch merge1 that changes the first line from some to will. I create branch merge2 that changes line 3 from words to work. I merge merge1 first and the merge2 second into master and everything work fine as expected.

Second Merge Test

I created two different branches with the file in the original state at the top of this post from master. I create branch merge1 that changes the first line from some to doesnt. I create branch merge2 that changes line 2 from random to work. I merge merge1 first and that works however when I try to merge merge2 I get a conflict error that I was not expecting.

When I open the file, this is what I see:

<<<<<<< HEAD
doesnt
random
=======
some
work
>>>>>>> merge2
words

What I don't understand is why the merge2 show the first line as some since merge2 did not change that line. This is ironically something the SVN handles fine.

Am I missing something here? Why can I have 2 different branches that are created from the same commit modify different lines of the small file just fine but not be able to change consecutive lines?

2
  • 3
    Personally, I find the default conflictstyle completely unreadable. Try git config --global merge.conflictstyle diff3. It may make more sense. Keep in mind, also, that merges work by applying patches (think Unified Diff format), which means that in such a small test, there may not be enough unaltered context lines to determine what needs to change. Sep 20, 2014 at 21:20
  • Thanks for the tip about conflictstyle, it diffidently gives more information to make a better informed decision on how to handle the conflict. I also understand the small scope of the file, originally had the issue on a 6000 line file but though this would be better for stackoverflow.
    – ryanzec
    Sep 20, 2014 at 21:51

1 Answer 1

5

Some speculation based on working with git...

Changes are tracked based on their surroundings. If you look at the diff for merge2's commit, it will look something like this:

 some
-random
+work
 words

When you try to merge that into the file modified by merge1, it can't find the anchor point "some". As a result, it needs to ask you how to proceed. Whereas when you modify nonconsecutive lines, it can still find some place in the file to anchor the changes it needs to apply.

Also, keep in mind that git is meant to tracking code changes. It tends to err on the side of caution when merging because it's better to have the user figure out the merge rather than automatically merge and break the code in some non-obvious way.

1
  • 1
    Good point about code changes and breakage...which makes the fact that SVN would merge this automatically just a little scary! :)
    – Wildcard
    Mar 17, 2016 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.