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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33308482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:39:16  来源:igfitidea点击:

Git: How configure KDiff3 as merge tool and diff tool

gitgit-mergekdiff3mergetool

提问by Joseph

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 Gitand KDiff3.

最近我在用GitExtension 2.46,但是同样的Git版本是1.9.4.msysgit.2。愿意只使用 Git 命令,我卸载了 GitExtension 并安装了最新版本的GitKDiff3

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'.

合并工具 kdiff3 不能用作“kdiff3”。

I guess it must be by the KDiff3 path.

我想它一定是通过 KDiff3 路径。

Environment

环境

  • OS: Windows 10
  • Git 2.6.1.windows.1
  • KDiff3 0.9.98 (64 bit)
  • 操作系统:Windows 10
  • Git 2.6.1.windows.1
  • KDiff3 0.9.98(64 位)

Questions:

问题:

  • What do I have to configure in the .gitconfig file for the command $ git mergetoolto open the KDiff3 GUI with the versions LOCAL, REMOTE, BASEand MERGEDof conflicted file?

  • How configure it to use it has diff-tool?

  • 我有什么在命令的文件的.gitconfig配置$ git mergetool与版本中打开KDiff3 GUI LOCALREMOTEBASE已合并冲突的文件吗?

  • 如何配置它以使用它具有差异工具?

回答by Joseph

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

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

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

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

trustExitCode选项的使用取决于您在 diff 工具返回时要执行的操作。从文档

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

git-difftool在每个文件上单独调用一个 diff 工具。默认情况下会忽略 diff 工具报告的错误。当调用的差异工具返回非零退出代码时,使用--trust-exit-code使git-difftool退出。

回答by Igor Kustov

Just to extend the @Joseph's answer:

只是为了扩展@Joseph 的回答

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

应用这些命令后,您的全局.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

回答by kris

For Mac users

对于 Mac 用户

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

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

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

(请注意,您可以复制并粘贴它并一次性运行它)

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

回答by user3159253

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

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

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:) ).

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

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

在Microsoft Windows,程序通常安装在专用的路径,所以你不能简单地键入kdiff3一个cmd会话,并得到KDiff3运行。

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 :)

硬性解决方案:您应该通过指定kdiff3.exe. 不幸的是,Git 不喜欢其配置中路径规范中的空格,所以上次我需要这个时,我最终得到了那些古老的 "C:\Progra~1...\kdiff3.exe" 好像已经晚了1990 年代 :)

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.

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

回答by martin

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.

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

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

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

[merge]
    tool = kdiff3

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

However, it works correctly now.

但是,它现在可以正常工作。

回答by VonC

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

要修改kris回答,从 Git 2.20(2018 年第四季度)开始,正确的命令git mergetool

git config --global merge.guitool kdiff3 

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

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

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)

提交 c217b93提交 57ba181提交 063f2bd(2018 年 10 月 24 日)由Denton Liu ( Denton-L)
(由Junio C gitsterHamano合并-- --提交 87c15d1,2018 年 10 月 30 日)

mergetool: accept -g/--[no-]guias arguments

In line with how difftoolaccepts a -g/--[no-]guioption, make mergetoolaccept the same option in order to use the merge.guitoolvariable to find the default mergetool instead of merge.tool.

mergetool: 接受-g/--[no-]gui作为参数

根据如何difftool接受-g/--[no-]gui选项,使mergetool接受相同的选项以使用merge.guitool变量来查找默认的合并工具而不是merge.tool.

回答by cola

(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)

(当我试图从 WSL git 中找出如何使用 kdiff3 时,我最终到了这里并得到了最后的部分,所以我会发布我的解决方案,供其他人在尝试找到答案时也绊倒在这里)

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

如何使用 kdiff3 作为 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$:

使用 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


Before Windows update 1903

Windows 更新 1903 之前

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

使用安装在 Windows 10 上的 kdiff3 作为 WSL 中 git 的差异/合并工具的步骤:

  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:
  1. 将 kdiff3 安装目录添加到 Windows Path。
  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. Convert unix-path to windows-path when calling kdiff3. Sample of my .gitconfig:
  1. 调用 kdiff3 时将 unix-path 转换为 windows-path。我的 .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