当我键入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

其他回答

我在Ubuntu上使用Kompare:

sudo apt-get install kompare

要比较两个分支:

git difftool -t kompare <my_branch> master

在查看了其他一些外部差异工具后,我发现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进行测试

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

git difftool -t

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

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

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

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

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

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

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

git difftool

你可以使用gitdifftool。

例如,如果您有Meld,可以通过以下方式编辑分支master和devel:

git config --global diff.external meld
git difftool master..devel