当我键入gitdiff时,我想用我选择的可视化diff工具(Windows上的SourceGear“diffmerge”)查看输出。如何配置git以执行此操作?


当前回答

以下内容可以从这里的其他答案中获得,但对我来说,这很难(信息太多),所以这里是tkdiff的“只需键入”答案:

git difftool --tool=tkdiff <path to the file to be diffed>

你可以用你最喜欢的diffing工具的可执行文件名来代替tkdiff。只要(例如tkdiff),(或您最喜欢的diffing工具)在您的PATH中,它就会启动。

其他回答

安装Meld:

 # apt-get install meld

然后选择它作为difftool:

 $ git config --global diff.tool meld

如果要在控制台中运行,请键入:

 $ git difftool

如果要使用图形模式,请键入:

 $ git mergetool

结果将是:

 'git mergetool' will now attempt to use one of the following tools:
 meld opendiff kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse
 diffmerge ecmerge p4merge araxis bc3 codecompare emerge vimdiff
 Merging:
 www/css/style.css
 www/js/controllers.js

 Normal merge conflict for 'www/css/style.css':
   {local}: modified file
   {remote}: modified file
 Hit return to start merge resolution tool (meld):

所以只需按Enter键即可使用meld(默认)。这将打开图形模式。进行神奇的保存并按下以解决合并。这就是全部。

在查看了其他一些外部差异工具后,我发现IntelliJ IDEA(和Android Studio)中的差异视图是我最喜欢的。

步骤1-设置IntelliJ IDEA以从命令行运行

如果要使用IntelliJ IDEA作为差异工具,应首先设置IntelliJ IDEA,以便按照以下说明从命令行运行:

在macOS或UNIX上:

确保IntelliJ IDEA正在运行。在主菜单上,选择“工具”|“创建命令行启动器”。将打开“创建启动器脚本”对话框,其中包含启动器脚本的建议路径和名称。您可以接受默认值,也可以指定自己的路径。注意它,因为你稍后会需要它。在IntelliJ IDEA之外,将启动器脚本的路径和名称添加到路径中。

在Windows上:

在Path系统环境变量中指定IntelliJ IDEA可执行文件的位置。在这种情况下,您将能够从任何目录调用IntelliJ IDEA可执行文件和其他IntelliJ IDEA命令。

步骤2-配置git以使用IntelliJ IDEA作为difftool

遵循此博客文章的说明:

Bash

export INTELLIJ_HOME /Applications/IntelliJ\ IDEA\ CE.app/Contents/MacOS
PATH=$IDEA_HOME $PATH

Fish

set INTELLIJ_HOME /Applications/IntelliJ\ IDEA\ CE.app/Contents/MacOS
set PATH $INTELLIJ_HOME $PATH

现在将以下内容添加到git配置中:

[merge]
   tool = intellij
[mergetool "intellij"]
   cmd = idea merge $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE") $(cd $(dirname "$BASE") && pwd)/$(basename "$BASE") $(cd $(dirname "$MERGED") && pwd)/$(basename "$MERGED")
   trustExitCode = true
[diff]
   tool = intellij
[difftool "intellij"]
   cmd = idea diff $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE")

您可以使用gitdifftool或gitdifftoolHEAD~1进行测试

这在Windows 7上适用。不需要中间sh脚本

.gitconfig的内容:

    [diff]
      tool = kdiff3

    [difftool]
       prompt = false

    [difftool "kdiff3"]
      path = C:/Program Files (x86)/KDiff3/kdiff3.exe
      cmd = "$LOCAL" "$REMOTE"

以下内容可以从这里的其他答案中获得,但对我来说,这很难(信息太多),所以这里是tkdiff的“只需键入”答案:

git difftool --tool=tkdiff <path to the file to be diffed>

你可以用你最喜欢的diffing工具的可执行文件名来代替tkdiff。只要(例如tkdiff),(或您最喜欢的diffing工具)在您的PATH中,它就会启动。

对于如何在1.6.3之前的Git版本上配置diff工具的Linux版本(1.6.3向Git添加了difftool),这是一个非常简洁的教程。

简而言之:

步骤1:将其添加到.gitconfig中

[diff]
  external = git_diff_wrapper
[pager]
  diff =

步骤2:创建一个名为git_diff_wrapper的文件,将其放在$PATH中的某个位置

#!/bin/sh

vimdiff "$2" "$5"