Configuring diff tool with .gitconfig

Git

Git Problem Overview


How do I configure Git to use a different tool for diffing with the .gitconfig file?

I have this in my .gitconfig:

[diff]
    tool = git-chdiff #also tried /bin/git-chdiff

It does not work; it just opens the regular command line diff. When I do

export GIT_EXTERNAL_DIFF=git-chdiff

then git diff will open up the external diffing tool (so I know the external diff tool script works fine). Do I have something wrong with my .gitconfig configuration for the diff tool?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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 https://stackoverflow.com/questions/255202/how-do-i-view-git-diff-output-with-visual-diff-program

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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

Solution 7 - Git

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

[alias]
    kdiff = difftool --tool kdiff3

Solution 8 - Git

Refer to Microsoft's VS Code Tips and Tricks. Just run these commands in your terminal:

git config --global merge.tool code

But firstly you need add the code command to your PATH environment variable.

Enter image description here

Solution 9 - Git

Almost all the solutions in the previous answers doesn't work with Git version 2

Mine: Git version = 2.28.0

Solution of the difftool: git config --global diff.tool vimdiff

After it, you can use it without any problems.

Solution 10 - Git

In Windows we need to run the git difftool --tool-help command to see the various options like:

    'git difftool --tool=<tool>' may be set to one of the following:
                    vimdiff
                    vimdiff2
                    vimdiff3

    The following tools are valid, but not currently available:
                    araxis
                    bc
                    bc3
                    codecompare
                    deltawalker
                    diffmerge
                    diffuse
                    ecmerge
                    emerge
                    examdiff
                    gvimdiff
                    gvimdiff2
                    gvimdiff3
                    kdiff3
                    kompare
                    meld
                    opendiff
                    p4merge
                    tkdiff
                    winmerge
                    xxdiff

Some of the tools listed above only work in a windowed
environment. If run in a terminal-only session, they will fail.

And we can add any of them (for example, WinMerge) like

git difftool --tool=winmerge

For configuring Notepad++ to see files before committing:

 git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

And using git commit will open the commit information in Notepad++.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionryanzecView Question on Stackoverflow
Solution 1 - GitOmer DaganView Answer on Stackoverflow
Solution 2 - GitFredrik PihlView Answer on Stackoverflow
Solution 3 - GittgozaView Answer on Stackoverflow
Solution 4 - GitDan RayView Answer on Stackoverflow
Solution 5 - GitRBTView Answer on Stackoverflow
Solution 6 - GitmoodboomView Answer on Stackoverflow
Solution 7 - GitAlexander KatzView Answer on Stackoverflow
Solution 8 - GitsudozView Answer on Stackoverflow
Solution 9 - GitILIASS BENView Answer on Stackoverflow
Solution 10 - GitGoyal VickyView Answer on Stackoverflow