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


当前回答

Windows/MSYS Git解决方案

看完答案后,我发现了一种更简单的方法,只需要更改一个文件。

使用参数2和5创建一个批处理文件来调用diff程序。此文件必须位于您路径中的某个位置。(如果您不知道它在哪里,请将其放在C:\windows中。)调用它,例如,“gitdiff.bat”。我的是:@回声消失REM这是gitdiff.bat“C:\Program Files\WinMerge\WinMergeU.exe”%2%5将环境变量设置为指向批处理文件。例如:GIT_EXTERNAL_DIFF=gitdiff.bat。或通过PowerShell键入GIT-config--globaldiff.EXTERNAL gitdiff.bat。重要的是不要使用引号或指定任何路径信息,否则将无法工作。这就是为什么gitdiff.bat必须在您的路径中。

现在,当您键入“gitdiff”时,它将调用外部diff查看器。

其他回答

如果您通过Cygwin执行此操作,则可能需要使用cygpath:

$ git config difftool.bc3.cmd "git-diff-bcomp-wrapper.sh \$LOCAL \$REMOTE"
$ cat git-diff-bcomp-wrapper.sh
#!/bin/sh
"c:/Program Files (x86)/Beyond Compare 3/BComp.exe" `cygpath -w $1` `cygpath -w $2`

我在这里尝试了一些新奇的东西(使用tkdiff),但没有任何效果。所以我写了下面的脚本,tkgitdiff。它做了我需要它做的事。

$ cat tkgitdiff
#!/bin/sh

#
# tkdiff for git.
# Gives you the diff between HEAD and the current state of your file.
#

newfile=$1
git diff HEAD -- $newfile > /tmp/patch.dat
cp $newfile /tmp
savedPWD=$PWD
cd /tmp
patch -R $newfile < patch.dat
cd $savedPWD
tkdiff /tmp/$newfile $newfile

对以前的伟大答案的简短总结:

git difftool --tool-help
git config --global diff.tool <chosen tool>
git config --global --add difftool.prompt false

然后通过键入(也可以选择指定文件名)使用它:

git difftool

如果你恰好已经有一个与文件类型相关的diff工具(比如,因为你安装了TortoiseSVN,它附带了一个diff查看器),你可以将常规的git diff输出通过管道发送到一个“temp”文件,然后直接打开该文件,而无需了解查看器:

git diff > "~/temp.diff" && start "~/temp.diff"

将其设置为全局别名效果更好:gitwhat

[alias]
    what = "!f() { git diff > "~/temp.diff" && start "~/temp.diff"; }; f"

如果你在Mac上,并且有Xcode,那么你已经安装了FileMerge。终端命令是opendiff,因此您可以执行以下操作:

git difftool -t opendiff