Git Mergetool – Merging With a GUI

Commands discussed in this section:

  • git mergetool

Merging With a GUI

Git can also work with a large number of GUI tools for helping with merging. A couple of popular GUI tools used on Linux are kdiff3 and meld. They are worth trying (you may need to install them if they aren’t installed yet).

Back To The Same Old Conflict

We’ll go back to the conflict in the the second line of the README file, from our repository as described in the Merging Branches With a Conflict page.

Merging with kdiff3

If you want to try out kdiff3 you could type:

$ git mergetool -t kdiff3
Merging the files: README

Normal merge conflict for 'README':
  {local}: modified
  {remote}: modified
Hit return to start merge resolution tool (kdiff3):

kdiff3

You can see 3 windows at the top and a large window on the bottom in the kdiff3 GUI. The 3 windows on the top, from left to right are:

  • The common ancestor version
  • The current branch (master)
  • The other branch (test)

The large window on the bottom is editable and is where the result of the merge is placed. kdiff3 makes merging easy. You can put your mouse over the Merge Conflict text and press the right mouse button, which will let you choose which version of text you want:

kdiff3select

You can then choose what you would like for the merge. For example, if you chose Select Line(s) from B, the window at the bottom with the merge result is changed to this:

kdiff3select B

When you are satisifed with the merged version of README, you can click on File–>Save and exit and then resolve the conflict by adding README to the index.

Merging with meld

If you want to try to use meld you can type:

$ git mergetool -t meld
Merging the files: README

Normal merge conflict for 'README':
  {local}: modified
  {remote}: modified

Hit return to start merge resolution tool (meld):

You would then see:

meld

With meld, three windows are shown, and left to right are:

    • The local branch (master branch)
    • The current version that you are merging into

The other (or “remote”) version (from the test branch)

To choose the text from the master branch, click on the black arrow in the left-most window at the top right. The resulting merge looks like this:

meld1

Clicking on a black arrow moves text to other file (meld copies the text to the other file, and removes the text from where it was). After you have saved the version of README, you can resolve the conflict in git.

Configure Git For Your Mergetool Of Choice

To configure git to remember which merge tool you want, type git config –global merge.tool [tool]. For example, if you want git mergetool to automatically use kdiff3 as our mergetool, we would type:

$ git config --global merge.tool kdiff3

There are many merge tools available and everybody has their own favorites.

Next: Remote repositories
Previous: Merging With a Conflict

Related:
Merging Branches Without a Conflict