如何查看“git diff” 使用我喜欢的差异工具/查看器输出?
当我输入 git diff 时,我想使用我选择的可视化 diff 工具(Windows 上的 SourceGear“diffmerge”)查看输出。 我如何配置 git 来做到这一点?
When I type git diff
, I want to view the output with my visual diff tool of choice (SourceGear "diffmerge" on Windows). How do I configure git to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
从 Git1.6.3 开始,您可以使用 git difftool 脚本:参见 我的回答如下。
可能是这样 文章会对您有所帮助。 以下是最好的部分:
有两种不同的方法来指定外部差异工具。
第一种是您使用的方法,通过设置 GIT_EXTERNAL_DIFF 变量。 但是,该变量应该指向可执行文件的完整路径。 此外,由 GIT_EXTERNAL_DIFF 指定的可执行文件将使用一组固定的 7 个参数进行调用:
由于大多数 diff 工具将需要不同的参数顺序(并且仅需要一些),因此您很可能必须指定一个包装器脚本,该脚本在Turn 调用真正的 diff 工具。
我更喜欢的第二种方法是通过“git”配置外部 diff 工具
这是我所做的:
1)创建一个包装脚本“git-diff-wrapper.sh”,其中包含类似的内容
如您所见,只有第二个(“旧文件”)和第五个( “新文件”)参数将是
传递给 diff 工具。
2)
在命令提示符下键入,替换为“git-diff-wrapper.sh”的路径,因此您的 ~/.gitconfig 包含
确保使用正确的语法来指定包装器脚本和 diff 的路径
工具,即使用正斜杠而不是反斜杠。 就我而言,我
在 .gitconfig 和
包装脚本中都有。 注意后面的“猫”!
(我认为只有某些可能无法返回正确或一致的返回状态的程序才需要“
| cat
”。如果您的 diff 工具具有明确的返回状态,您可能需要尝试不使用尾随的 cat )(Diomidis Spinellis 添加了在评论中:
(上面引用的文章)是外部工具通过配置文件定义的理论(而不是通过环境变量)。
实际中(还是外部工具的配置文件定义),可以参考:
Since Git1.6.3, you can use the git difftool script: see my answer below.
May be this article will help you. Here are the best parts:
There are two different ways to specify an external diff tool.
The first is the method you used, by setting the GIT_EXTERNAL_DIFF variable. However, the variable is supposed to point to the full path of the executable. Moreover, the executable specified by GIT_EXTERNAL_DIFF will be called with a fixed set of 7 arguments:
As most diff tools will require a different order (and only some) of the arguments, you will most likely have to specify a wrapper script instead, which in turn calls the real diff tool.
The second method, which I prefer, is to configure the external diff tool via "git
config". Here is what I did:
1) Create a wrapper script "git-diff-wrapper.sh" which contains something like
As you can see, only the second ("old-file") and fifth ("new-file") arguments will be
passed to the diff tool.
2) Type
at the command prompt, replacing with the path to "git-diff-wrapper.sh", so your ~/.gitconfig contains
Be sure to use the correct syntax to specify the paths to the wrapper script and diff
tool, i.e. use forward slashed instead of backslashes. In my case, I have
in .gitconfig and
in the wrapper script. Mind the trailing "cat"!
(I suppose the '
| cat
' is needed only for some programs which may not return a proper or consistent return status. You might want to try without the trailing cat if your diff tool has explicit return status)(Diomidis Spinellis adds in the comments:
That (the article quoted above) is the theory for external tool defined through config file (not through environment variable).
In practice (still for config file definition of external tool), you can refer to:
完成我之前的 "diff .external”配置答案上面:
如Jakub提到,Git1.6.3引入了 git difftool,最初于 2008 年 9 月提出:
USAGE=
'[--tool=tool] [--commit=ref] [--start=ref --end= ref] [--no-prompt] [要合并的文件]'
(请参阅本答案最后部分的
--extcmd
)$LOCAL
包含起始版本中的文件内容,$REMOTE
包含最终修订版中文件的内容。$BASE
包含文件中的内容最后一个用例是当您想将当前工作树与其他工作树进行比较时比 HEAD (例如标签)
注意:从 Git 2.5 开始,
git config diff.tool winmerge
就足够了!请参阅“git mergetool winmerge”
和从 Git 1.7.11 开始,您可以选择
--dir-diff
,以便生成外部 diff 工具,该工具可以在填充两个临时目录后一次比较两个目录层次结构,而不是每个文件对运行一次外部工具的实例。Git 2.5 之前:
使用自定义 diff 工具配置
difftool
的实际案例:将 winmerge.sh 存储在 PATH 的目录部分中:
如果您有其他工具(kdiff3、P4Diff,...),创建另一个 shell 脚本和适当的
difftool.myDiffTool.cmd
配置指令。然后您可以使用
diff.tool
配置轻松切换工具。您还有 Dave 的博客文章 添加其他详细信息。
(或者这个问题用于
winmergeu
选项)此设置的有趣之处在于
winmerge.sh
脚本:您可以自定义它以考虑特殊情况。例如,请参阅 David Marble 的 回答下面的示例,处理:
正如 Kem Mason 在 < a href="https://stackoverflow.com/questions/255202/how-do-i-view-git-diff-output-with-visual-diff-program/4881489#4881489">他的答案,您还可以使用
--extcmd
选项避免使用任何包装器:例如,这就是
gitk
能够运行/使用任何diff
工具。To complete my previous "diff.external" config answer above:
As mentioned by Jakub, Git1.6.3 introduced git difftool, originally proposed in September 2008:
USAGE=
'[--tool=tool] [--commit=ref] [--start=ref --end=ref] [--no-prompt] [file to merge]'
(See
--extcmd
in the last part of this answer)$LOCAL
contains the contents of the file from the starting revision and$REMOTE
contains the contents of the file in the ending revision.$BASE
contains the contents of the file in the worThe last use case is when you'd like to compare your current worktree to something other than HEAD (e.g. a tag)
Note: since Git 2.5,
git config diff.tool winmerge
is enough!See "git mergetool winmerge"
And since Git 1.7.11, you have the option
--dir-diff
, in order to to spawn external diff tools that can compare two directory hierarchies at a time after populating two temporary directories, instead of running an instance of the external tool once per a file pair.Before Git 2.5:
Practical case for configuring
difftool
with your custom diff tool:With winmerge.sh stored in a directory part of your PATH:
If you have another tool (kdiff3, P4Diff, ...), create another shell script, and the appropriate
difftool.myDiffTool.cmd
config directive.Then you can easily switch tools with the
diff.tool
config.You have also this blog entry by Dave to add other details.
(Or this question for the
winmergeu
options)The interest with this setting is the
winmerge.sh
script: you can customize it to take into account special cases.See for instance David Marble's answer below for an example which deals with:
As Kem Mason mentions in his answer, you can also avoid any wrapper by using the
--extcmd
option:For instance, this is how
gitk
is able to run/use anydiff
tool.尝试这个解决方案:
Meld 理解 Git 并提供围绕最近更改的导航。
Try this solution:
Meld understands Git and provides navigating around the recent changes.
使用新的 git difftool,只需将其添加到 .gitconfig 文件中即可:(
可选)还可以添加:
另请查看 diffall ,我编写的一个简单脚本,用于扩展串行打开每个文件的烦人的(IMO)默认差异行为。
Windows 上的全局 .gitconfig 位于
%USERPROFILE%\.gitconfig
With new git difftool, its as simple as adding this to your .gitconfig file:
Optionally, also add:
Also check out diffall, a simple script I wrote to extend the annoying (IMO) default diff behaviour of opening each in serial.
Global .gitconfig on Windows is in
%USERPROFILE%\.gitconfig
从 Git 版本 1.6.3 开始,有“
目前(在撰写本答案时)开箱即用的支持有KDiff3, Kompare,tkdiff、Meld、xxdiff、emerge、vimdiff、gvimdiff、ecmerge、漫反射 和 opendiff; 如果您要使用的工具不在此列表中,您始终可以使用“
difftool..cmd
”配置选项。“git difftool”接受与“git diff”相同的选项。
Since Git version 1.6.3 there is "git difftool" which you can configure to use your favorite graphical diff tool.
Currently supported (at the time of writing this answer) out-of-the-box are KDiff3, Kompare, tkdiff, Meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, Diffuse and opendiff; if the tool you want to use isn't on this list, you can always use '
difftool.<tool>.cmd
' configuration option."git difftool" accepts the same options as "git diff".
我对此还有一点补充。 我喜欢定期使用不支持作为默认工具之一的 diff 应用程序(例如万花筒),通过
我也喜欢让默认的
diff
只是常规命令行,因此设置GIT_EXTERNAL_DIFF
变量不是一个选项。您可以通过此命令一次性使用任意
diff
应用程序:它只是将 2 个文件传递给您指定的命令,因此您可能也不需要包装器。
I have one addition to this. I like to regularly use a diff app that isn't supported as one of the default tools (e.g. kaleidoscope), via
I also like to have the default
diff
just be the regular command line, so setting theGIT_EXTERNAL_DIFF
variable isn't an option.You can use an arbitrary
diff
app as a one-off with this command:It just passes the 2 files to the command you specify, so you probably don't need a wrapper either.
构建于 VonC 的答案 要处理文件删除和添加,请使用以下命令和脚本:
这与将其放入全局文件.gitconfig相同:
然后将以下内容放入文件 >winmerge.sh 必须位于您的路径上:
Building on VonC's answer to deal with file removals and additions, use the following commands and scripts:
Which is the same as putting this in your global file .gitconfig:
Then put the following in file winmerge.sh which must be on your path:
Windows/MSYS Git 的解决方案
阅读答案后,我发现了一种更简单的方法,涉及只更改一个文件。
使用参数 2 和 5 创建一个批处理文件来调用 diff 程序。该文件必须位于您的路径中的某个位置。 (如果您不知道它在哪里,请将其放在C:\windows中。)例如,将其命名为“gitdiff.bat”。 我的是:
设置环境变量以指向您的批处理文件。 例如:
GIT_EXTERNAL_DIFF=gitdiff.bat
。 或者通过 PowerShell 输入git config --global diff.external gitdiff.bat
。重要的是不要使用引号,或指定任何路径信息,否则它将无法工作。 这就是为什么 gitdiff.bat 必须在您的路径中。
现在,当您输入“git diff”时,它将调用您的外部差异查看器。
Solution for Windows/MSYS Git
After reading the answers, I discovered a simpler way that involves changing only one file.
Create a batch file to invoke your diff program, with argument 2 and 5. This file must be somewhere in your path. (If you don't know where that is, put it in C:\windows.) Call it, for example, "gitdiff.bat". Mine is:
Set the environment variable to point to your batch file. For example:
GIT_EXTERNAL_DIFF=gitdiff.bat
. Or through PowerShell by typinggit config --global diff.external gitdiff.bat
.It is important to not use quotes, or specify any path information, otherwise it won't work. That's why gitdiff.bat must be in your path.
Now when you type "git diff", it will invoke your external diff viewer.
如果您通过 Cygwin 执行此操作,则可能需要使用 cygpath:
If you're doing this through Cygwin, you may need to use cygpath:
在查看了其他一些外部 diff 工具后,我发现 IntelliJ IDEA(和 Android Studio)中的
diff
视图对我来说是最好的。步骤 1 - 设置 IntelliJ IDEA 从命令行运行
如果您想使用 IntelliJ IDEA 作为 diff 工具,您应该首先按照说明设置 IntelliJ IDEA 从命令行运行 此处:
在 macOS 上或 UNIX:
请注意它,因为稍后您将需要它。
在 IntelliJ IDEA 外部,将启动器脚本的路径和名称添加到您的路径中。
在 Windows 上:
步骤 2 - 配置 git 以使用 IntelliJ IDEA 作为 diff 工具
按照 这篇博文:
Bash
Fish
现在将以下内容添加到您的 git 配置中:
您可以使用
尝试一下git difftool
或git difftool HEAD~1
After looking at some other external diff tools, I found that the
diff
view in IntelliJ IDEA (and Android Studio) is the best one for me.Step 1 - setup IntelliJ IDEA to be run from the command line
If you want to use IntelliJ IDEA as your diff tool you should first setup IntelliJ IDEA to be run from the command line following the instructions here:
On macOS or UNIX:
Tools | Create Command-line Launcher
. The dialog box Create Launcher Script opens, with the suggested path and name of the launcher script. You can accept default, or specify your own path.Make notice of it, as you'll need it later.
Outside of IntelliJ IDEA, add the path and name of the launcher script to your path.
On Windows:
Step 2 - configure git to use IntelliJ IDEA as the difftool
Following the instructions on this blog post:
Bash
Fish
Now add the following to your git config:
You can try it out with
git difftool
orgit difftool HEAD~1
之前的精彩答案的简短摘要:
然后通过键入(也可以选择指定文件名)来使用它:
A short summary of the previous great answers:
Then use it by typing (optionally specifying the file name as well):
这对我在 Windows 7 上有效。 不需要任何中间 sh 脚本
的内容。 git配置:
This works for me on Windows 7. There isn't any need for intermediary sh scripts
Contents of .gitconfig:
安装 Meld:
然后选择它作为 difftool:
如果您想在控制台中运行它,请输入:
如果您想要使用图形模式,请键入:
输出将为:
因此只需按 Enter 即可使用 meld(默认)。 这将打开图形模式。 进行神奇的保存并按下即可解决合并问题。 就这样。
Install Meld:
Then choose that as the difftool:
If you want to run it in the console, type:
If you want to use graphic mode, type:
And the output would be:
So just press Enter to use meld (default). This would open graphic mode. Make the magic save and press that that resolve the merge. That's all.
这是适用于 Windows 的批处理文件 - 假设 DiffMerge 安装在默认位置,处理 x64,根据需要向前处理反斜杠替换,并且能够自行安装。 应该很容易用您最喜欢的 diff 程序替换 DiffMerge。
安装:
gitvdiff.bat:
Here's a batch file that works for Windows - assumes DiffMerge installed in default location, handles x64, handles forward to backslash replacement as necessary and has ability to install itself. Should be easy to replace DiffMerge with your favourite diff program.
To install:
gitvdiff.bat:
如果您使用的是 Mac 并且有 Xcode,那么您就有 FileMerge 已安装。 终端命令是 opendiff,所以你可以这样做:
If you're on a Mac and have Xcode, then you have FileMerge installed. The terminal command is opendiff, so you can just do:
简介
作为参考,我想在 VonC 的回答。 请记住,我使用的是 MSys 版本的 Git(此时为 1.6.0.2)并修改了 PATH,并从 PowerShell(或 cmd.exe)而不是 Bash shell 运行 Git 本身。
我引入了一个新命令,
gitdiff
。 运行此命令会暂时重定向 git diff 以使用您选择的视觉 diff 程序(而不是永久执行此操作的 VonC 解决方案)。 这使我能够同时拥有默认的 Git diff 功能 (git diff
) 和可视化 diff 功能 (gitdiff
)。 两个命令都采用相同的参数,因此例如要直观地比较特定文件中的更改,您可以键入Setup
请注意,
$GitInstall
用作安装 Git 的目录的占位符。创建一个新文件,
$GitInstall\cmd\gitdiff.cmd
<前><代码> @echo 关闭
设置本地
for /F "delims=" %%I in ("%~dp0..") do @set path=%%~fI\bin;%%~fI\mingw\bin;%PATH%
如果“%HOME%”==“”@set HOME=%USERPROFILE%
设置 GIT_EXTERNAL_DIFF=git-diff-visual.cmd
设置 GIT_PAGER=cat
git 差异%*
终端本地
创建一个新文件
$GitInstall\bin\git-diff-visual.cmd
(替换 < code>[visual_diff_exe] 占位符,包含您选择的 diff 程序的完整路径)<前><代码> @echo 关闭
rem diff 由 git 调用,有 7 个参数:
rem 路径 旧文件 旧十六进制 旧模式 新文件 新十六进制 新模式
echo 比较“%5”
“[visual_diff_exe]”“%2”“%5”
出口0
现在你已经完成了。 从 Git 存储库中运行 gitdiff 现在应该为每个已更改的文件调用可视化 diff 程序。
Introduction
For reference I'd like to include my variation on VonC's answer. Keep in mind that I am using the MSys version of Git (1.6.0.2 at this time) with modified PATH, and running Git itself from PowerShell (or cmd.exe), not the Bash shell.
I introduced a new command,
gitdiff
. Running this command temporarily redirectsgit diff
to use a visual diff program of your choice (as opposed to VonC's solution that does it permanently). This allows me to have both the default Git diff functionality (git diff
) as well as visual diff functionality (gitdiff
). Both commands take the same parameters, so for example to visually diff changes in a particular file you can typeSetup
Note that
$GitInstall
is used as a placeholder for the directory where Git is installed.Create a new file,
$GitInstall\cmd\gitdiff.cmd
Create a new file,
$GitInstall\bin\git-diff-visual.cmd
(replacing[visual_diff_exe]
placeholder with full path to the diff program of your choice)You're now done. Running
gitdiff
from within a Git repository should now invoke your visual diff program for every file that was changed.有关如何在 1.6.3 之前的 Git 版本上配置 diff 工具的 Linux 版本(1.6.3 将 difftool 添加到 Git),这是一个很棒的简洁教程。
简而言之:
第 1 步:将其添加到您的 .gitconfig
第 2 步:创建一个名为 git_diff_wrapper 的文件,将其放在 $PATH 中的某个位置
For a Linux version of how to configure a diff tool on Git versions prior to 1.6.3 (1.6.3 added difftool to Git), this is a great concise tutorial.
In brief:
Step 1: add this to your .gitconfig
Step 2: create a file named git_diff_wrapper, put it somewhere in your $PATH
在 Mac OS X 上,
在 Git 文件夹中为我完成这项工作。 要安装 Diffuse,可以使用端口 -
On Mac OS X,
does the job for me in the Git folder. For installing Diffuse, one can use port -
以下内容可以从这里的其他答案中收集到,但对我来说这很困难(信息太多),所以这里是 tkdiff 的“只需输入”答案:
您可以用您最喜欢的比较工具的可执行名称替换 tkdiff。 只要(例如 tkdiff)(或您最喜欢的比较工具)在您的 PATH 中,它就会启动。
The following can be gleaned from the other answers here, but for me it's difficult, (too much information), so here's the 'just type it in' answer for tkdiff:
You can substitute the executable name of your favorite diffing tool for tkdiff. As long as (e.g. tkdiff), (or your favorite diffing tool) is in your PATH, it will be launched.
您可以使用 git difftool 。
例如,如果您有 Meld,则可以编辑分支
master
和开发者:
You can use
git difftool
.For example, if you have Meld, you can edit the branches
master
anddevel
by:我在这里尝试了一些奇特的东西(使用 tkdiff),但没有任何效果。 所以我写了下面的脚本,tkgitdiff。 它做了我需要它做的事情。
I tried the fancy stuff here (with tkdiff) and nothing worked for me. So I wrote the following script, tkgitdiff. It does what I need it to do.
我已经在文件
~/.gitconfig
中使用这一点很长时间了:使用
git-meld
:但现在我厌倦了总是使用 Meld 在图形环境中,使用此设置调用正常差异并不简单,因此我切换到此设置:
通过此设置,事情就像这样的工作:
我仍然可以保留旧的
git diff
。I've been using this bit in file
~/.gitconfig
for a long time:With
git-meld
:But now I got tired of always using Meld in a graphical environment, and it's not trivial to invoke the normal diff with this setup, so I switched to this:
With this setup, things like this work:
And I still get to keep the good old
git diff
.我在 Ubuntu 上使用 Kompare:
比较两个分支:
I use Kompare on Ubuntu:
To compare two branches:
如果您碰巧已经有一个与文件类型关联的 diff 工具(例如,因为您安装了带有 diff 查看器的 TortoiseSVN),您可以将常规的 git diff 输出通过管道传输到“临时”文件,然后只需直接打开该文件,无需了解有关查看器的任何信息:
将其设置为全局别名效果更好:
git What
If you happen to already have a diff tool associated with filetypes (say, because you installed TortoiseSVN which comes with a diff viewer) you could just pipe the regular
git diff
output to a "temp" file, then just open that file directly without needing to know anything about the viewer:Setting it as a global alias works even better:
git what
如果您不熟悉命令行,那么如果您安装 TortoiseGit,您可以右键单击在文件上获取带有“稍后比较”选项的 TortoiseGit 子菜单。
当您在第一个文件上选择此选项时,您可以右键单击第二个文件,转到 TortoiseGit 子菜单并选择“Diff with ==yourfilehere==”。
这将为 TortoiseGit 合并 GUI 提供结果。
If you're not one for the command line then if you install TortoiseGit, you can right click on a file to get a TortoiseGit submenu with the "Diff later" option.
When you select this on the first file, you can then right click on the second file, go to the TortoiseGit submenu and select "Diff with ==yourfilehere==".
This will give the TortoiseGit merge GUI for the result.
您可能想尝试 xd,它是 Git/SVN diff 的 GUI 包装器。 它本身不是一个差异工具。
当您想要运行
git diff
或svn diff
时,您运行xd
,它会向您显示文件列表、预览窗口,您可以启动您喜欢的任何差异工具,包括 tkdiff、xxdiff、gvimdiff、Emacs (ediff)、XEmacs (ediff),Meld,漫反射、Kompare 和 KDiff3。 您还可以运行任何自定义工具。不幸的是,该工具不支持 Windows。
披露:我是这个工具的作者。
You may want to try out xd, which is a GUI wrapper for Git/SVN diff. It is not a diff tool itself.
You run
xd
when you want to rungit diff
orsvn diff
and it will show you a list of files, a preview window and you can launch any diff tool you like, including tkdiff, xxdiff, gvimdiff, Emacs (ediff), XEmacs (ediff), Meld, Diffuse, Kompare and KDiff3. You can also run any custom tool.Unfortunately the tool doesn't support Windows.
Disclosure: I am the author of this tool.