Pimp My Git - Git Mergetool

I like to work with git on the command line. But in some cases I prefer UI support. For example, solving merge conflicts is such a case. Git has a command mergetool, which can open a graphical tool to solve merge conflicts. But before you can use this command, you had to configure it. In this blog post I'd like to show you how to configure mergetool and how to use it.

Configuration

First at all, open a shell on Linux. On Windows, open Git Bash. Then choose a graphic tool that should support you solving merge conflicts. git mergetool --tool-help shows a list which tools are supported on your machine

 1sparsick@sparsick-ThinkPad-T430s > git mergetool --tool-help
 2'git mergetool --tool=<tool>' may be set to one of the following:
 3               araxis
 4               kdiff3
 5               meld
 6
 7The following tools are valid, but not currently available:
 8               bc
 9               bc3
10               codecompare
11               deltawalker
12               diffmerge
13               diffuse
14               ecmerge
15               emerge
16               gvimdiff
17               gvimdiff2
18               gvimdiff3
19               opendiff
20               p4merge
21               tkdiff
22               tortoisemerge
23               vimdiff
24               vimdiff2
25               vimdiff3
26               winmerge
27               xxdiff
28
29Some of the tools listed above only work in a windowed
30environment. If run in a terminal-only session, they will fail.

This command shows two lists. The first list shows all tools that are supported by git and that are available on your machine (in sample, it is araxis, kdiff3 and meld). The second list shows that are also supported by git, but they aren't install on your machine. I use meld as graphical tool. It's runnable on Windows and Linux. If you haven't install meld on your machine, then it's now the right time to do it or choose another tool. We want to set mergetool globally for all our repositories.

1sparsick@sparsick-ThinkPad-T430s > git config --global merge.tool meld
2sparsick@sparsick-ThinkPad-T430s > git mergetool
3No files need merging

If git mergetool returns more than No files need merging, then the path to your graphic tool isn't set in your $PATH system variable (The normal case on Windows systems). It's possible to set the path to the graphical tool directly in git.

1sparsick@sparsick-ThinkPad-T430s > git config --global mergetool.meld.path /c/Program\ Files\ \(x86\)/Meld/Meld.exe

Bear two important things in mind: mergetool is written without a dot between merge and tool and meld is a placeholder for the name of the graphical tool in the above sample. If you use another tool such like vimdiff, then the config key is called mergetool.vimdiff.path. Now git mergetool is ready to use.

Usage

Now I'd like to demonstrate how to use git mergetool. It is used in when we have merge conflicts during a merge action. Let's say we want to merge branch branch1 into master and this merge will have some merge conflicts.

1sparsick@sparsick-ThinkPad-T430s > git merge branch1
2
3Auto-merging test
4CONFLICT (content): Merge conflict in test
5Automatic merge failed; fix conflicts and then commit the result.

Now, we want to solve these conflicts with a graphical tool (in the example, it's meld). git mergetool on the command line open the graphical tool of our choice.

1sparsick@sparsick-ThinkPad-T430s > git mergetool
2
3Merging:
4test
5
6Normal merge conflict for 'test':
7{local}: modified file
8{remote}: modified file

After solving the merge conflicts, the change has to commit.

 1sparsick@sparsick-ThinkPad-T430s > git status
 2
 3On branch master
 4All conflicts fixed but you are still merging.
 5(use "git commit" to conclude merge)
 6
 7Changes to be committed:
 8
 9modified:   test
10
11Untracked files:
12(use "git add <file>..." to include in what will be committed)
13
14test.orig
15sparsick@sparsick-ThinkPad-T430s > git commit

You can see that we have a new untracked file test.orig . This is a backup of the merged file created by mergetool. You can configure that this backup should be removed after a successful merge.

1
2sparsick@sparsick-ThinkPad-T430s > git config --global mergetool.keepBackup false

Further files are created when using git mergetool:

 1sparsick@sparsick-ThinkPad-T430s > git status
 2
 3On branch master
 4Untracked files:
 5(use "git add ..." to include in what will be committed)
 6
 7test.BACKUP.7344
 8test.BASE.7344
 9test.LOCAL.7344
10test.REMOTE.7344

If only these files are currently untracked, then a git clean can help. Otherwise they have to be removed manually.

1sparsick@sparsick-ThinkPad-T430s > git clean -f
2
3Removing test.BACKUP.7344
4Removing test.BASE.7344
5Removing test.LOCAL.7344
6Removing test.REMOTE.734
  1. Meld Homepage
  2. git mergetool Documentation