Do you ever find it difficult figuring out complex merges? “Wait, was my change the part after the <<<<<<< or before the >>>>>>> ?”, “What did I do here and what did they do?”, “Oh my gawd, was this whole file modified?!?” If you find yourself asking these questions, maybe you need to use git mergetool.

Back when I used darcs, and was the integrator of our project, I used to wish I could see the diff in a tool other than just my text editor, something like kdiff3, meld, or (my personal favourite) vimdiff.

To tell git to use vimdiff as your mergetool run git config --global merge.tool vimdiff.

If I replay one of the git conflicting merges and run git mergetool, I see this:

Example vimdiff output

(To recreate this yourself, in a clone of the git repository, execute git checkout -b test 7a937b2e7378699553b8f88d995f9e54e722a3c0 and git merge b468f0ce4881bf42ffc820b1cddad67dad17fd80)

In this case, the file as I knew it is on the left and the right section is the modifications made in the remote branch. You want to modify and save the center panel (ie. the one with the actual filename of the file with merge conflicts).

git mergetool is controlled by the command line, even if you’re using a graphical tool. What I mean is that it displays status and sometimes prompts you in the terminal that is was run in, even if you’re using a tool like meld or kdiff3.

If you run git mergetool without specify a file, it will pick the first one in the list and not let you move on till you’ve merged it completely. Be aware that it considers a file merged if it has been modified and the exit code of the merging program indicates that everything went well and marks the file as merged (I don’t currently know a way to unmark a merge conflicted file as merged).

Aside from these drawbacks, git mergetool makes it very simple to better visualize the differences in files when doing a manual merge.

git mergetool takes two optional arguments: -t and a list of filenames. -t lets you specify a mergetool without having to set any config options. If you pass a list of filenames to mergetool, it will only try to merge those files (assuming they’re marked as having merge confilicts).


About these ads