Like it!

Join me on Facebook!

Like it!

How to resolve git conflicts for good

Merge changes from multiple developers across the same code

I have a simple text file containing some words, namely a quote from Edgar Allan Poe:

I wish I could write as mysterious as a cat.

It lies quietly in my local 'master' branch and in the remote one (origin/master). One day, for some reason, another guy working with me on the project pushed a commit that changes that line into:

I wish I could write as elegant as a cat.

As you may see she changed the word "mysterious" with "elegant". Now when I try to merge the remote 'master' branch with my own local 'master' branch I get a weird conflict. Opening the simple text file would reveal the following horror:

I wish I could write as
<<<<<<< HEAD
mysterious
=======
elegant
>>>>>>> origin/master
as a cat.

That's a conflict-marked file: we both modified the same line and GIT doesn't know what version should keep. Let's try to fix it for good.

Reading and fixing a conflict-marked file in GIT

A conflict-marked area begins with <<<<<<< and ends with >>>>>>>. These are the conflict markers. The two conflicting blocks themselves are divided by a =======.

The part between the lines beginning <<<<<<< and ====== is what I already have locally. The lines between ======= and >>>>>>> is what was introduced by the other commit, in this case origin/master.

To resolve the conflict, just delete the conflict markers and type out what you want to keep. In my case I would keep 'mysterious', as it is the original quote. The text file would become:

I wish I could write as mysterious as a cat.

Finally git add the file, commit the change with a new commit message, and you are done.

Sources

GitHub - Resolving a merge conflict from the command line (link)
Stackoverflow - Git conflict markers (link)

comments