Marc Hughes

I am a developer from a bit west of Boston.

Resolving git merge conflicts

20 Mar 2013

Dealing with merge conflicts with git isn't too hard, but I keep losing work because other people do it incorrectly. Here's the easiest way to do it:

First, set up a merge tool. I like p4merge since I'm used to it, but feel free to grab any that git supports. Here a link on how to do that.

Now, every time you want to inflict your changes on others, follow this procedure...

git add (your files)

This causes any changes you've made to go into your staging area.

git commit

This causes your changes to go into your local repository. Make sure to put a meaningful commit message in!

git pull

This goes out and grabs everyone else's changes since you last synced. If you get a merge-conflict message, do the next two steps. If not, skip down to the push.

git mergetool

This will step you through the files that had merge conflicts, one by one, and help you fix them. Assuming you set up a merge tool, you'll have a nice graphical way of doing this. Make sure to save each file after you fix it.

git commit

This commits your merge. You will see ALL the files that you and the other person edited here, not just the files you edited. That's normal. Don't try to remove the other person's changes.

This is the one that people mess up all the time, especially when using a graphical git tool. They might see dozens of files they never touched. Panic. And try to revert them all. But by doing that, they are telling git that they know better than it and this merge shouldn't contain those changes, effectively losing all the changes the other person made.

git push

This pushes the changes you made, and the merge you just did to the central repo for everyone to get.