Sunday, December 19, 2010

How to setup KDiff as the diff tool for GIT

The git diff command does a great job of showing what has changed.  But it shows this information on the command prompt.  Some people who are addicted to nice and pretty GUI's, might get bogged down because of this.

Do not worry you people, there is a nice GUI based option.  KDiff3 is the answer to this problem!  This post will show how easily we can integrate the KDiff3 tool with GIT.

KDiff3 had nice and easy GUI.  It does its job very well.  It might not be the prettiest but its extremely simple and intuitive to use.  +1 for KDiff3 from my side!

So how do we integrate KDiff3 with GIT?

How do they do it?

GIT can be integrated easily with any third party diff tools.  We will integrate GIT to with KDiff3.  The simple steps to follow are
  1. Download and install KDiff3 from here.
  2. GIT needs to know that KDiff3 should be used as the preferred diff/merge tool.  For this, we need to make a simple change in the .gitconfig file.  This file can be found under your home directory.
Lets look at the second step in more detail.  On a windows machine the .gitconfig file is found under C:\Users\<your user name>\ directory.  This path can also be referred via the shortcut ~ on the GIT prompt.

Open the .gitconfig file in your favorite text editor

It should look something like this

Add the following lines to the file

The path config property under the mergetool and difftool, should point to the installation path of KDiff3 tool on your machine.  The updated .gitconfig file should look somewhat like this

NOTE: please use forward slash "/" as the path separator even on windows machines.  Using back slash "\" will not work!

The above config tells GIT to use the KDiff3 tool as the external diff/merge tool.

All set!  Lights, Camera, Action!

Lets edit a few files in a GIT repository:

Lets view the difftool in action.

GIT will ask your permission to launch KDiff3 for viewing the test1.txt.
Once you exit the KDiff3 view of test1.txt, it will ask your permission to launch KDiff3 for test2.txt

Hitting enter will launch the KDiff3 again for viewing test2.txt
Basically, GIT will launch the KDiff3 for all the files that have changes since the last commit.

If you feel annoyed about GIT asking your permission for showing the KDiff3 for each changed file, use the following command

This command will launch the KDiff3 for each edited file, without any prompt!

To use KDiff as the merge tool use the following command

Hitting enter will launch the KDiff3 as the merge tool
KDiff3 shows nice GUI to do the merge easily.  It shows the original file on the leftmost window called "A" or "Base", local file in the middle called "B" or "Local" and remote file in the rightmost window called "C" or "Remote".

As you can see its pretty trivial to use KDiff3 as the external diff/merge tool with GIT.  Have fun with GIT!  Go GIT it!

Have some Fun!