60

When there's a merge conflict in Git, junk like the following is inserted into the conflicting files. Three questions:

  1. How do you read these annotations?
  2. What are some strategies to use when fixing these merge conflicts?
  3. Is there a GUI tool for Mac that knows how to read these files and display the two versions side-by-side to make it easier to fix the problem?

enter image description here

Note: In case it's relevant, I am using GitHub's Mac GUI client.

1

5 Answers 5

36

Everything between <<<<<< and ====== comes from the HEAD revision, which is the committed state before starting the merge operation (git merge will complain if the tree is dirty, so it should be equivalent to your working directory).

The parts between ====== and >>>>>> come from the version being merged. The text after the >>>>>> is the comment of the commit that introduced the conflicting change.

That there is a conflict marker means that the base version of this part of the file is different from both "new" versions. The base version (last common ancestor) is not shown.

If you want a more comfortable merge and have a GUI available, I suggest you take a look at kdiff3.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Simon. Is there not some GUI app that can open these files and display the HEAD and the merged version side-by-side?
Can you point me to which information in that link is useful? The git manual is inaccessible to me.
@ChristopherJamesCalo, kdiff3 is a GUI tool.
22

There's now a better solution than trying to read the gibberish markers that Git inserts into your files. Kaleidoscope 2, Black Pixel's second release of their diff tool, is now also a merge tool. It's not free, but it works beautifully.

After you install Kaleidoscope 2, it's really simple to integrate with Git. Open Kaleidoscope and from the Menu Bar choose Kaleidoscope > Integration…

Kaleidoscope's Integration menu option.

Then you'll see this window. Simply choose Git from the left nav and then install the ksdiff command-line tool and then make Kaleidoscope Git's default diff and merge tool.

Kaleidoscope's Git Integration dialog.

Finally, once you have a merge conflict in Git, simply go to the command line and execute git mergetool. Now the conflict is easy to read. Enjoy.

Kaleidoscope sane view for merge conflicts.

2 Comments

@FrankFang you just need to close window when you all conflicts were resolved. You will be asked then if the merge was ok.
It's an alternative solution if better is subjective. If you are on macOS you may also try the build in merge tool opendiff (part of Xcode tools); set "opendiff" as the default mergetool globally: git config --global merge.tool opendiff, see also stackoverflow.com/a/25625663/880188 EDIT: or just see below ;)
17
<<<< HEAD #Where the conflict starts #Previous Revision ========== # The point where things look iffy #Things that changed >>>>> New Commit # Point where the conflict ends 

Mac's Xcode comes with FileMerge which can be accessed on the command line with opendiff.

git mergetool -t opendiff

2 Comments

What does it mean if there was nothing for #Things that changed, but I still got a conflict?
git mergetool -t opendiff was the answer for me. Thanks.
4

You can configure a (graphical) merge tool and use that tool to do the conflict resolution.

Also have a look at the git mergetool command - if you have one of the pre-defined tools installed or have configured some other tool, it will open up the tool for resolution http://schacon.github.com/git/git-mergetool.html

If you are interested in GUI tools, you need not worry about what the notations really mean, as the GUI tool will help you to easily make the resolutions. Just understand that the parts marked with ===== and >>>>> are the conflict sections.

1 Comment

Can you give me an example of a Mac GUI app that can read these files and explain how to open them? I've tried opening these files in different diff and merge tools, and they just open as a single text file instead of being interpreted as two files. (Which is the whole purpose of the conflict markers.)
0

When you have two branches with changes to the same file and you try to merge them, a merge conflict will occur. To see the list of conflicted files run git status on your terminal.

Conflicted lines of the files will be marked with visual indicators: <<<<< - The conflict starts after this line. ===== - Devides changes from HEAD and merging_branch. >>>>> - End of the conflicted lines.

<<<<<<< HEAD conflicted text from HEAD ======= conflicted text from merging_branch >>>>>>> merging_branch 

When you fix your conflicted files and you are ready to merge, all you have to do is run git add and git commit to generate the merge commit. Once the commit was made ,git push the changes to the branch.

Reference article: Git merge.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.