How can I configure KDiff3 as a merge tool and diff tool for git?
Asked Answered
H

8

284

Recently I was using GitExtension 2.46, but the Git version that has the same is 1.9.4.msysgit.2. Willing to use only Git commands, I uninstalled GitExtension and install the latest version available of Git and KDiff3.

When I make a merge and have conflicts, I run the following command:

git mergetool

Then I receive the message:

The merge tool kdiff3 is not available as 'kdiff3'.

I guess it must be by the KDiff3 path.

Environment

  • OS: Windows 10
  • Git 2.6.1.windows.1
  • KDiff3 0.9.98 (64 bit)

Questions:

  • What do I have to configure in the .gitconfig file for the command git mergetool to open the KDiff3 GUI with the versions LOCAL, REMOTE, BASE and MERGED of conflicted file?

  • How can I configure it to use it as a diff tool?

Hassiehassin answered 23/10, 2015 at 17:46 Comment(2)
Related post - How to configure a diff tool in Git in general.Obviate
With Git 2.33+ (Q3 2021), git config --global merge.tool kdiff3 will be enough. See my updated answer belowQuoth
H
469

These sites were very helpful, almost, mergetool and difftool. I used the global configuration, but can be used by repository without problems. You just need to execute the following commands:

git config --global merge.tool kdiff3
git config --global mergetool.kdiff3.path "C:/Program Files/KDiff3/bin/kdiff3.exe"
git config --global mergetool.kdiff3.trustExitCode false

git config --global diff.guitool kdiff3
git config --global difftool.kdiff3.path "C:/Program Files/KDiff3/bin/kdiff3.exe"
git config --global difftool.kdiff3.trustExitCode false

Note that the latest version kdiff3 moved the executable from the root of the application folder C:/Program Files/KDiff3 into the bin/ folder inside the application folder. If you're using an older version, remove "bin/" from the paths above.

The use of the trustExitCode option depends on what you want to do when diff tool returns. From documentation:

git-difftool invokes a diff tool individually on each file. Errors reported by the diff tool are ignored by default. Use --trust-exit-code to make git-difftool exit when an invoked diff tool returns a non-zero exit code.

Hassiehassin answered 26/10, 2015 at 14:56 Comment(9)
Still, why would I want git-difftool not to exit if kdiff3 fails?Athal
For me to have the diff tool recognized by Visual Studio 2015, I had to change this line git config --global --add diff.guitool kdiff3 to this : git config --global --add diff.tool kdiff3Lei
@DavidTorres Probably because badly behaved Windows tools (that exit with non-zero codes on success) ruin it for everyone.Overeager
According to the referenced docs setting trustExitCode to false is unnecessary, since the default is to ignore it anyway.Misbelief
AFAIK, --add will add a second or third entry when invoked multiple times. That's hard to fix later, because it can't simply be removed with --remove. Just setting a value without --add should be ok.Britain
Good point @ThomasWeller, the answer was updated to reflect your suggestionHassiehassin
Is there a way to default to KDiff3 when typing 'git diff'?Themistocles
Lol, I gave up on this one, but you made it work withing seconds. Big Thank YouLynelllynelle
Latest versions at download.kde.org/stable/kdiff3.Mellifluent
P
77

Just to extend the @Joseph's answer:

After applying these commands your global .gitconfig file will have the following lines (to speed up the process you can just copy them in the file):

[merge]
    tool = kdiff3
[mergetool "kdiff3"]
    path = C:/Program Files/KDiff3/kdiff3.exe
    trustExitCode = false
[diff]
    guitool = kdiff3
[difftool "kdiff3"]
    path = C:/Program Files/KDiff3/kdiff3.exe
    trustExitCode = false
Pyrosis answered 26/11, 2016 at 10:28 Comment(4)
@Alex78191, my answer reflects the Joseph's answer and there you can find more details about this setting.Pyrosis
It took me a long time to get this right. 2 things led me astray: (1) The .gitconfig file I was editing was not the one being used. See stackoverflow.com/questions/2114111/… for identifying the one(s) being loaded. (2) Don't mix and match cmd = and path = in gitconfig, TL;DR: delete cmd and just use pathMisbelief
Now on git bash use .... git difftool <filename> or simple git difftool to run the diff gui kdiff3 you just set.Crosspiece
for visual studio also add this:[diff] tool = kdiff3Diphase
G
47

For Mac users

Here is @Joseph's accepted answer, but with the default Mac install path location of kdiff3

(Note that you can copy and paste this and run it in one go)

git config --global merge.tool kdiff3 
git config --global mergetool.kdiff3.path  "/Applications/kdiff3.app/Contents/MacOS/kdiff3" 
git config --global mergetool.kdiff3.trustExitCode false

git config --global diff.guitool kdiff3
git config --global difftool.kdiff3.path "/Applications/kdiff3.app/Contents/MacOS/kdiff3"
git config --global difftool.kdiff3.trustExitCode false
Gentleness answered 23/10, 2018 at 4:28 Comment(3)
Don't use --add since that may result in 2 config entries if you run the command twice. It's a mess cleaning this up, because you can't delete a single entry any more. See git-scm.com/docs/git-config: "Multiple lines can be added to an option"Britain
It not that hard to delete single entries, just open the .gitconfig and delete themCusped
This doesn't works for me, kdiff start and close immediately without giving me the opportunity to actually solve the conflictInexorable
Q
17

Update 2021:

With Git 2.33 (Q3 2021), on Windows, mergetool has been taught to find kdiff3.exe just like it finds winmerge.exe.

git config --global merge.tool kdiff3 is enough.

See commit 47eb4c6 (07 Jun 2021) by Michael Schindler (michaelcompressconsult).
(Merged by Junio C Hamano -- gitster -- in commit b7bd70d, 08 Jul 2021)

mergetools/kdiff3: make kdiff3 work on Windows too

Signed-off-by: Michael Schindler [email protected]

The native kdiff3 mergetool is not found by git mergetool(man) on Windows.
The message "The merge tool kdiff3 is not available as 'kdiff3'" is displayed.

Just like we translate the name of the binary and look for it on the search path for WinMerge, do the same for kdiff3 to find it.


2018:

To amend kris' answer, starting with Git 2.20 (Q4 2018), the proper command for git mergetool will be

git config --global merge.guitool kdiff3 

That is because "git mergetool" learned to take the "--[no-]gui" option, just like "git difftool" does.

See commit c217b93, commit 57ba181, commit 063f2bd (24 Oct 2018) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster -- in commit 87c15d1, 30 Oct 2018)

mergetool: accept -g/--[no-]gui as arguments

In line with how difftool accepts a -g/--[no-]gui option, make mergetool accept the same option in order to use the merge.guitool variable to find the default mergetool instead of merge.tool.

Quoth answered 4/11, 2018 at 3:43 Comment(0)
I
13

Well, the problem is that Git can't find KDiff3 in the %PATH%.

In a typical Unix installation all executables reside in several well-known locations (/bin/, /usr/bin/, /usr/local/bin/, etc.), and one can invoke a program by simply typing its name in a shell processor (e.g. cmd.exe :) ).

In Microsoft Windows, programs are usually installed in dedicated paths so you can't simply type kdiff3 in a cmd session and get KDiff3 running.

The hard solution: you should tell Git where to find KDiff3 by specifying the full path to kdiff3.exe. Unfortunately, Git doesn't like spaces in the path specification in its config, so the last time I needed this, I ended up with those ancient "C:\Progra~1...\kdiff3.exe" as if it was late 1990s :)

The simple solution: Edit your computer settings and include the directory with kdiff3.exe in %PATH%. Then test if you can invoke it from cmd.exe by its name and then run Git.

Inward answered 23/10, 2015 at 18:2 Comment(0)
W
9

I needed to add the command line parameters or KDiff3 would only open without files and prompt me for base, local and remote. I used the version supplied with TortoiseHg.

Additionally, I needed to resort to the good old DOS 8.3 file names.

[merge]
    tool = kdiff3

[mergetool "kdiff3"]
    cmd = /c/Progra~1/TortoiseHg/lib/kdiff3.exe $BASE $LOCAL $REMOTE -o $MERGED

However, it works correctly now.

Wept answered 26/6, 2018 at 7:21 Comment(0)
U
7

(When trying to find out how to use kdiff3 from WSL git I ended up here and got the final pieces, so I'll post my solution for anyone else also stumbling in here while trying to find that answer)

How to use kdiff3 as diff/merge tool for WSL git

With Windows update 1903 it is a lot easier; just use wslpath and there is no need to share TMP from Windows to WSL since the Windows side now has access to the WSL filesystem via \wsl$:

[merge]
    renormalize = true
    guitool = kdiff3
[diff]
    tool = kdiff3
[difftool]
    prompt = false
[difftool "kdiff3"]
    # Unix style paths must be converted to windows path style
    cmd = kdiff3.exe \"`wslpath -w $LOCAL`\" \"`wslpath -w $REMOTE`\"
    trustExitCode = false
[mergetool]
    keepBackup = false
    prompt = false
[mergetool "kdiff3"]
    path = kdiff3.exe
    trustExitCode = false

Before Windows update 1903

Steps for using kdiff3 installed on Windows 10 as diff/merge tool for git in WSL:

  1. Add the kdiff3 installation directory to the Windows Path.
  2. Add TMP to the WSLENV Windows environment variable (WSLENV=TMP/up). The TMP dir will be used by git for temporary files, like previous revisions of files, so the path must be on the windows filesystem for this to work.
  3. Set TMPDIR to TMP in .bashrc:
# If TMP is passed via WSLENV then use it as TMPDIR
[[ ! -z "$WSLENV" && ! -z "$TMP" ]] && export TMPDIR=$TMP
  1. Convert unix-path to windows-path when calling kdiff3. Sample of my .gitconfig:
[merge]
    renormalize = true
    guitool = kdiff3
[diff]
    tool = kdiff3
[difftool]
    prompt = false
[difftool "kdiff3"]
    #path = kdiff3.exe
    # Unix style paths must be converted to windows path style by changing '/mnt/c/' or '/c/' to 'c:/'
    cmd = kdiff3.exe \"`echo $LOCAL | sed 's_^\\(/mnt\\)\\?/\\([a-z]\\)/_\\2:/_'`\" \"`echo $REMOTE | sed 's_^\\(/mnt\\)\\?/\\([a-z]\\)/_\\2:/_'`\"
    trustExitCode = false
[mergetool]
    keepBackup = false
    prompt = false
[mergetool "kdiff3"]
    path = kdiff3.exe
    trustExitCode = false
Untouched answered 14/4, 2019 at 20:10 Comment(0)
E
7

Same as accepted answer but with new installation path for easy copy/paste:

git config --global merge.tool kdiff3
git config --global mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global mergetool.kdiff3.trustExitCode false

git config --global diff.guitool kdiff3
git config --global difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global difftool.kdiff3.trustExitCode false
Efik answered 6/3, 2022 at 0:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.