Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring diff tool with .gitconfig

Tags:

git

People also ask

How do I set git Difftool?

Use the diff tool specified by <tool>. Valid values include emerge, kompare, meld, and vimdiff. Run git difftool --tool-help for the list of valid <tool> settings. If a diff tool is not specified, git difftool will use the configuration variable diff.

What is the default git diff tool?

The default Diff Tool is vimdiff. Specifying a Diff Tool affects the git difftool command. The command git diff still performs diffing on the command-line.


An additional way to do that (from the command line):

git config --global diff.tool tkdiff
git config --global merge.tool tkdiff
git config --global --add difftool.prompt false

The first two lines will set the difftool and mergetool to tkdiff- change that according to your preferences. The third line disables the annoying prompt so whenever you hit git difftool it will automatically launch the difftool.


Git offers a range of difftools pre-configured "out-of-the-box" (kdiff3, kompare, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, diffuse, opendiff, p4merge and araxis), and also allows you to specify your own. To use one of the pre-configured difftools (for example, "vimdiff"), you add the following lines to your ~/.gitconfig:

[diff]
    tool = vimdiff

Now, you will be able to run "git difftool" and use your tool of choice.

Specifying your own difftool, on the other hand, takes a little bit more work, see How do I view 'git diff' output with my preferred diff tool/ viewer?


Others have done a 99% answer on this, but there is one step left out. (My answer will be coming from OS X so you will have to change file paths accordingly.)

You make these changes to your ~/.gitconfig:

[diff]
    tool = diffmerge
[difftool "diffmerge"]
    cmd = /Applications/Diffmerge.app/Contents/MacOS/diffmerge $LOCAL $REMOTE

This will fix the diff tool. You can also fix this without editing the ~/.gitconfig directly by entering these commands from the terminal:

git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd "/Applications/DiffMerge.appContents/MacOS/diffmerge \$LOCAL \$REMOTE"

The 1% that everyone else failed to mention is when using this you can't just run git diff myfile.txt; you need to run git difftool myfile.txt.


Here's the part of my ~/.gitconfig where I configure diff and merge tools. I like diffmerge by SourceGear. (I like it very very much, as a matter of fact).

[merge]
        tool = diffmerge
[mergetool "diffmerge"]
        cmd = "diffmerge --merge --result=\"$MERGED\" \"$LOCAL\" \"$(if test -f \"$BASE\"; then echo \"$BASE\"; else echo \"$LOCAL\"; fi)\" \"$REMOTE\""
        trustExitCode = false
[diff]
        tool = diffmerge
[difftool "diffmerge"]
        cmd = diffmerge \"$LOCAL\" \"$REMOTE\"

So, you see, you're defining a tool named "diffmerge" in the [difftool "diffmerge"] line. Then I'm setting the tool "diffmerge" as the default in the [diff] tool = section.

I obviously have the "diffmerge" command in my path, here. Otherwise I'd need to give a full path to the executable.


Reproducing my answer from this question which was more specific to setting Beyond Compare as diff tool for Git. All the details that I've shared are equally useful for any diff tool in general, so I am sharing it here:

The first command that we run is as below:

git config --global diff.tool bc3

The above command creates the below entry in file .gitconfig found in the %userprofile% directory:

[diff]
    tool = bc3

Then you run below command (Running this command is redundant in this particular case and is required in some specialized cases only. You will know it in a short while):

git config --global difftool.bc3.path "c:/program files/beyond compare 3/bcomp.exe"

The above command creates the below entry in the .gitconfig file:

[difftool "bc3"]
    path = c:/program files/Beyond Compare 3/bcomp.exe

The thing to know here is the key bc3. This is a well-known key to Git corresponding to a particular version of well-known comparison tools available in the market (bc3 corresponds to the third version of the Beyond Compare tool). If you want to see all pre-defined keys just run git difftool --tool-help command on Git Bash. It returns the below list:

vimdiff
vimdiff2
vimdiff3
araxis
bc
bc3
codecompare
deltawalker
diffmerge
diffuse
ecmerge
emerge
examdiff
gvimdiff
gvimdiff2
gvimdiff3
kdiff3
kompare
meld
opendiff
p4merge
tkdiff
winmerge
xxdiff

You can use any of the above keys or define a custom key of your own. If you want to setup a new tool altogether (or a newly released version of well-known tool) which doesn't map to any of the keys listed above then you are free to map it to any of keys listed above or to a new custom key of your own.

What if you have to setup a comparison tool which is

  • Absolutely new in the market

Or

  • A new version of an existing well-known tool has got released which is not mapped to any pre-defined keys in Git

Like in my case, I had installed Beyond Compare 4. Beyond Compare is a well-known tool to Git, but its version 4 release is not mapped to any of the existing keys by default. So you can follow any of the below approaches:

  1. I can map Beyond Compare 4 tool to already existing key bc3 which corresponds to Beyond Compare 3 version. I didn't have Beyond Compare version 3 on my computer, so I didn't care. If I wanted I could have mapped it to any of the pre-defined keys in the above list also, e.g., examdiff.

    If you map a well-known version of tools to appropriate already existing/well- known key then you would not need to run the second command as their install path is already known to Git.

    For example., if I had installed Beyond Compare version 3 on my box then having the below configuration in my .gitconfig file would have been sufficient to get going:

    [diff]
    tool = bc3
    

    But if you want to change the default associated tool then you end up mentioning the path attribute separately, so that Git gets to know the path from where you new tool's EXE file has to be launched. Here is the entry which forces Git to launch Beyond Compare 4 instead. Note the EXE file's path:

    [difftool "bc3"]
    path = c:/program files/Beyond Compare 4/bcomp.exe
    
  2. The cleanest approach is to define a new key altogether for the new comparison tool or a new version of a well-known tool. Like in my case I defined a new key bc4, so that it is easy to remember. In such a case you have to run two commands in all, but your second command will not be setting the path of your new tool's executable. Instead you have to set the cmd attribute for your new tool as shown below:

    git config --global diff.tool bc4
    
    git config --global difftool.bc4.cmd "\"C:\\Program Files\\Beyond Compare 4\\bcomp.exe\" -s \"\$LOCAL\" -d \"\$REMOTE\""
    

    Running the above commands creates the below entries in your .gitconfig file:

    [diff]
    tool = bc4
    [difftool "bc4"]
    cmd = \"C:\\Program Files\\Beyond Compare 4\\bcomp.exe\" -s \"$LOCAL\" -d \"$REMOTE\"
    

I would strongly recommend you to follow approach # 2 to avoid any confusion for yourself in future.


Adding one of the blocks below works for me to use KDiff3 for my Windows and Linux development environments. It makes for a nice consistent cross-platform diff and merge tool.

Linux

[difftool "kdiff3"]
    path = /usr/bin/kdiff3
    trustExitCode = false
[difftool]
    prompt = false
[diff]
    tool = kdiff3
[mergetool "kdiff3"]
    path = /usr/bin/kdiff3
    trustExitCode = false
[mergetool]
    keepBackup = false
[merge]
    tool = kdiff3

Windows

[difftool "kdiff3"]
    path = C:/Progra~1/KDiff3/kdiff3.exe
    trustExitCode = false
[difftool]
    prompt = false
[diff]
    tool = kdiff3
[mergetool "kdiff3"]
    path = C:/Progra~1/KDiff3/kdiff3.exe
    trustExitCode = false
[mergetool]
    keepBackup = false
[merge]
    tool = kdiff3

If you want to have an option to use multiple diff tools, add an alias to file .gitconfig:

[alias]
    kdiff = difftool --tool kdiff3