Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Pimp My Git – Git Mergetool

DZone's Guide to

Pimp My Git – Git Mergetool

Even those who prefer to use Git via the command line sometimes like using graphical tools to handle merges. Learn how to get Git's mergetool command can help.

· Agile Zone ·
Free Resource

You've been hearing a lot about agile software development, get started with the eBook: Agile Product Development from 321 Gang

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

Configuration

First of 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 of tools that are supported on your machine

sparsick@sparsick-ThinkPad-T430s > git mergetool --tool-help
'git mergetool --tool=<tool>' may be set to one of the following:
                araxis
                kdiff3
                meld
 
The following tools are valid, but not currently available:
                bc
                bc3
                codecompare
                deltawalker
                diffmerge
                diffuse
                ecmerge
                emerge
                gvimdiff
                gvimdiff2
                gvimdiff3
                opendiff
                p4merge
                tkdiff
                tortoisemerge
                vimdiff
                vimdiff2
                vimdiff3
                winmerge
                xxdiff
 
Some of the tools listed above only work in a windowed
environment. If run in a terminal-only session, they will fail.


This command shows two lists. The first list shows all the tools that are supported by git and that are available on your machine (in the sample above, it is araxis, kdiff3 and meld). The second list shows those that are also supported by git, but that aren't installed on your machine.

I use meld as my graphical tool. It’s runnable on Windows and Linux. If you haven’t installed meld on your machine, then now is the right time to do it or choose another tool.

We want to set mergetool globally for all our repositories.

sparsick@sparsick-ThinkPad-T430s > git config --global merge.tool meld
sparsick@sparsick-ThinkPad-T430s > git mergetool
No 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.

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

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 as 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 instances when we have merge conflicts during a merge action. Let’s say we want to merge a branch, branch1, into master and this merge will have some merge conflicts.

sparsick@sparsick-ThinkPad-T430s > git merge branch1
 
Auto-merging test
CONFLICT (content): Merge conflict in test
Automatic 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 opens the graphical tool of our choice.

sparsick@sparsick-ThinkPad-T430s > git mergetool
 
Merging:
test
 
Normal merge conflict for 'test':
{local}: modified file
{remote}: modified file


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

sparsick@sparsick-ThinkPad-T430s > git status
 
On branch master
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
 
Changes to be committed:
 
modified:   test
 
Untracked files:
(use "git add <file>..." to include in what will be committed)
 
test.orig
sparsick@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.

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


Further files are created when using git mergetool:

sparsick@sparsick-ThinkPad-T430s > git status
 
On branch master
Untracked files:
(use "git add ..." to include in what will be committed)
 
test.BACKUP.7344
test.BASE.7344
test.LOCAL.7344
test.REMOTE.7344


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

sparsick@sparsick-ThinkPad-T430s > git clean -f
 
Removing test.BACKUP.7344
Removing test.BASE.7344
Removing test.LOCAL.7344
Removing test.REMOTE.734


Links

  1. Meld Homepage
  2. git mergetool Documentation

Download the free agile tools checklist from 321 Gang. This guide will help you choose the right agile tools to position your team for success. 

Topics:
git ,agile ,merge ,commit ,bash

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}