Git: 如何将 KDiff3 配置为合并工具和差异工具?

Git: How can I configure KDiff3 as a merge tool and diff tool?

最近我在使用GitExtension 2.46,但具有相同功能的Git版本是1.9.4.msysgit.2。只愿意使用 Git 命令,我卸载了 GitExtension 并安装了 Git and KDiff3.

的最新版本

当我进行合并并发生冲突时,我运行执行以下命令:

git mergetool

然后我收到消息:

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

我猜一定是通过KDiff3路径。

环境

问题:

嗯,问题是 Git 在 %PATH% 中找不到 KDiff3。

在典型的 Unix 安装中,所有可执行文件都驻留在几个众所周知的位置(/bin//usr/bin//usr/local/bin/ 等),并且可以通过简单地调用程序在 shell 处理器中输入其名称(例如 cmd.exe :))。

在 Microsoft Windows 中,程序通常安装在专用路径中,因此您不能简单地在 cmd 会话中键入 kdiff3 并获取 KDiff3 运行ning。

硬性解决方案:您应该通过指定 kdiff3.exe 的完整路径来告诉 Git 在哪里可以找到 KDiff3。不幸的是,Git 不喜欢其配置中路径规范中的空格,所以上次我需要这个时,我以那些古老的 "C:\Progra~1...\kdiff3.exe" 结束,就好像它是 1990 年代后期:)

简单的解决方案:编辑您的计算机设置并在 %PATH% 中包含带有 kdiff3.exe 的目录。然后测试是否可以通过名称从 cmd.exe 调用它,然后 运行 Git.

这些网站非常有用,几乎 mergetool and difftool。我使用了全局配置,但可以毫无问题地被存储库使用。您只需要执行以下命令:

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

请注意,最新版本的 kdiff3 将可执行文件从应用程序文件夹 C:/Program Files/KDiff3 的根目录移动到应用程序文件夹内的 bin/ 文件夹中。如果您使用的是旧版本,请从上面的路径中删除“bin/”。

trustExitCode 选项的使用取决于 diff 工具 returns 时您想做什么。来自 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.

只是扩展:

应用这些命令后,您的全局 .gitconfig 文件将包含以下行 (要加快进程,您可以将它们复制到文件中):

[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

我需要添加命令行参数,否则 KDiff3 只会在没有文件的情况下打开并提示我输入基本、本地和远程。我使用了 TortoiseHg.

提供的版本

此外,我需要求助于旧的 DOS 8.3 文件名。

[merge]
    tool = kdiff3

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

不过,它现在可以正常工作了。

对于 Mac 用户

这是@Joseph 接受的答案,但默认 Mac 安装路径位置 kdiff3

(请注意,您可以复制并粘贴此内容并 运行 一次完成)

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

git config --global --add diff.guitool kdiff3
git config --global --add difftool.kdiff3.path "/Applications/kdiff3.app/Contents/MacOS/kdiff3"
git config --global --add difftool.kdiff3.trustExitCode false

2021 年更新:

随着 Git 2.33(2021 年第 3 季度),在 Windows 上,mergetool 被教导可以像找到 winmerge.exe 一样找到 kdiff3.exe

git config --global merge.tool kdiff3 够了。

参见 commit 47eb4c6 (07 Jun 2021) by Michael Schindler (michaelcompressconsult)
(由 Junio C Hamano -- gitster -- in commit b7bd70d 合并,2021 年 7 月 8 日)

mergetools/kdiff3: make kdiff3 work on Windows too

Signed-off-by: Michael Schindler michael@compressconsult.com

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:

要修改 kris' ,从 Git 2.20(2018 年第 4 季度)开始,git mergetool 的正确命令将是

git config --global merge.guitool kdiff3 

那是因为“git mergetool”学会了取“--[no-]gui”选项,就像 “git difftool”是。

参见 commit c217b93, commit 57ba181, commit 063f2bd (24 Oct 2018) by Denton Liu (Denton-L)
(由 Junio C Hamano -- gitster -- in commit 87c15d1 合并,2018 年 10 月 30 日)

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.

(当试图从 WSL git 中找出如何使用 kdiff3 时,我最终来到这里并得到了最后的部分,所以我 post 我的解决方案适用于其他也在这里跌跌撞撞的人在寻找答案时)

如何将 kdiff3 用作 WSL diff/merge 工具 git

使用 Windows 更新 1903 就容易多了;只需使用 wslpath,无需将 TMP 从 Windows 共享到 WSL,因为 Windows 端现在可以通过 \wsl$:

访问 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

在 Windows 更新 1903

之前

使用 Windows 10 上安装的 kdiff3 作为 WSL 中 git 的 diff/merge 工具的步骤:

  1. 将kdiff3安装目录添加到Windows路径。
  2. 将 TMP 添加到 WSLENV Windows 环境变量 (WSLENV=TMP/up)。 TMP 目录将被 git 用于临时文件,就像以前的文件修订版一样,因此路径必须在 windows 文件系统上才能工作。
  3. 在 .bashrc 中将 TMPDIR 设置为 TMP:
# If TMP is passed via WSLENV then use it as TMPDIR
[[ ! -z "$WSLENV" && ! -z "$TMP" ]] && export TMPDIR=$TMP
  1. 调用 kdiff3 时将 unix 路径转换为 ​​windows 路径。我的示例 .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

与已接受的答案相同,但使用新的安装路径以方便 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