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


当前回答

如果你恰好已经有一个与文件类型相关的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"

其他回答

安装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(默认)。这将打开图形模式。进行神奇的保存并按下以解决合并。这就是全部。

如果您通过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`

我已经在文件~/.gitconfig中使用这个位很长时间了:

[diff]
    external = ~/Dropbox/source/bash/git-meld

使用git meld:

#!/bin/bash
if [ "$DISPLAY" = "" ];
then
    diff $2 $5
else
    meld $2 $5
fi

但是现在我已经厌倦了在图形环境中使用Meld,使用这个设置调用普通的diff并不是件容易的事,所以我切换到了这个:

[alias]
    v =  "!sh -c 'if [ $# -eq 0 ] ; then git difftool -y -t meld ; else git difftool -y $@ ; fi' -"

使用此设置,类似于这样的工作:

git v
git v --staged
git v -t kompare
git v --staged -t tkdiff

我仍然能保持好的老样子。

我在Ubuntu上使用Kompare:

sudo apt-get install kompare

要比较两个分支:

git difftool -t kompare <my_branch> master

这是一个适用于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