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


当前回答

这是一个适用于Windows的批处理文件-假设DiffMerge安装在默认位置,处理x64,根据需要处理前向反斜杠替换,并能够自行安装。应该很容易用你喜欢的diff程序替换DiffMerge。

要安装:

gitvdiff --install 

gitvdiff.bat:

@echo off

REM ---- Install? ----
REM To install, run gitvdiff --install

if %1==--install goto install



REM ---- Find DiffMerge ----

if DEFINED ProgramFiles^(x86^) (
    Set DIFF="%ProgramFiles(x86)%\SourceGear\DiffMerge\DiffMerge.exe"
) else (
    Set DIFF="%ProgramFiles%\SourceGear\DiffMerge\DiffMerge.exe"
)



REM ---- Switch forward slashes to back slashes ----

set oldW=%2
set oldW=%oldW:/=\%
set newW=%5
set newW=%newW:/=\%


REM ---- Launch DiffMerge ----

%DIFF% /title1="Old Version" %oldW% /title2="New Version" %newW%

goto :EOF



REM ---- Install ----
:install
set selfL=%~dpnx0
set selfL=%selfL:\=/%
@echo on
git config --global diff.external %selfL%
@echo off


:EOF

其他回答

这是一个适用于Windows的批处理文件-假设DiffMerge安装在默认位置,处理x64,根据需要处理前向反斜杠替换,并能够自行安装。应该很容易用你喜欢的diff程序替换DiffMerge。

要安装:

gitvdiff --install 

gitvdiff.bat:

@echo off

REM ---- Install? ----
REM To install, run gitvdiff --install

if %1==--install goto install



REM ---- Find DiffMerge ----

if DEFINED ProgramFiles^(x86^) (
    Set DIFF="%ProgramFiles(x86)%\SourceGear\DiffMerge\DiffMerge.exe"
) else (
    Set DIFF="%ProgramFiles%\SourceGear\DiffMerge\DiffMerge.exe"
)



REM ---- Switch forward slashes to back slashes ----

set oldW=%2
set oldW=%oldW:/=\%
set newW=%5
set newW=%newW:/=\%


REM ---- Launch DiffMerge ----

%DIFF% /title1="Old Version" %oldW% /title2="New Version" %newW%

goto :EOF



REM ---- Install ----
:install
set selfL=%~dpnx0
set selfL=%selfL:\=/%
@echo on
git config --global diff.external %selfL%
@echo off


:EOF

从Git 1.6.3版开始,有了“gitdifftool”,您可以将其配置为使用您最喜欢的图形diff工具。

目前(在撰写此答案时)开箱即用支持KDiff3、Kompare、tkdiff、Meld、xxdiff、emerge、vimdiff、gvimdiff、ecmerge、Diffuse和opendiff;如果您要使用的工具不在此列表中,您可以始终使用difftool<tool>.cmd'配置选项。

“gitdifftool”接受与“gitdiff”相同的选项。

我在这里尝试了一些新奇的东西(使用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

对于如何在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"

我还有一个补充。我喜欢经常使用不支持作为默认工具之一的diff应用程序(例如万花筒),通过

git difftool -t

我还希望将默认diff作为常规命令行,因此设置GIT_EXTERNAL_diff变量不是一个选项。

您可以使用以下命令一次性使用任意diff应用程序:

git difftool --extcmd=/usr/bin/ksdiff

它只将2个文件传递给您指定的命令,因此您可能也不需要包装器。