我如何查看git diff'使用我喜欢的diff工具/查看器输出?
当我键入 git diff
时,我想使用我选择的Visual diff工具(Windows上的sourcegear“ diffmerge”)查看输出。如何配置git做到这一点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
当我键入 git diff
时,我想使用我选择的Visual diff工具(Windows上的sourcegear“ diffmerge”)查看输出。如何配置git做到这一点?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(26)
这是一个适用于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版本(目前为1.6.0.2),并从PowerShell(或CMD.EXE)(而不是bash shell)运行git本身。
我引入了一个新命令,
gitdiff
。运行此命令暂时重定向git diff
使用您选择的Visual diff程序(与永久执行的VONC解决方案相反)。这使我同时拥有默认的git diff功能(git diff
)以及Visual diff功能(gitdiff
)。这两个命令都采用相同的参数,因此,例如,要在特定文件中视觉上的差异更改,您可以输入设置
注意,请注意,
$ gitinstall
被用作安装git的目录的占位符。创建一个新文件,
$ gitinstall \ cmd \ gitdiff.cmd
创建一个新文件,
$ gitinstall \ bin \ bin \ git-diff-visual.cmd.cmd
代码> [visual_diff_exe] 占位符,带有您选择的DIFF程序的完整路径)您现在完成了。从git存储库中运行
gitdiff
现在应为已更改的每个文件调用您的Visual 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.对于Linux版本,如何在1.6.3之前在GIT版本上配置DIFF工具(1.6.3添加到Git中),这是一个很棒的简洁教程。
而
简
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的“ Just In tot It”答案:
您可以将您喜欢的扩散工具的可执行名称替换为TKDIFF。只要(例如TKDIFF)(或您最喜欢的扩散工具)在您的路径中,它将被启动。
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
和DEVEL
: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
:但是现在我厌倦了总是使用 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
.我使用 kompare 在ubuntu上:
比较两个分支:
I use Kompare on Ubuntu:
To compare two branches:
如果您碰巧已经有一个与Filetypes关联的DIFF工具(例如,因为您安装了带有diff查看器的totoisesvn),则可以将常规
git diff
输出输出到“ temp”文件,然后只需直接打开该文件而无需了解有关查看器的任何信息:将其设置为全局别名的工作效果更好:
git what 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
如果您不是命令行之一在文件上以使用“以后的diff”选项获取totoisegit子菜单。
当您在第一个文件上选择此内容时,您可以右键单击第二个文件,转到Tortoisegit子菜单,然后选择“ diff with == yourfilehere ==”。
这将为结果提供乌龟合并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包装器。它不是 一个差异工具本身。
您运行
XD
要运行git diff
或svn diff
,它将显示一个文件列表,一个预览窗口,您可以启动您喜欢的任何差异工具,包括tkdiff,xxdiff,gvimdiff,emacs(ediff), meld , diffuse , kompare“> kompare”> kompare http://kdiff3.sourceforge.net/“ rel =“ nofollow noreferrer”> 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.
由于Git1.6.3,您可以使用 git difftool脚本:请参阅我的答案)。
可能是此文章将为您提供帮助。这是最好的部分:
有两种不同的方法来指定外部差异工具。
第一个是通过设置GIT_EXTERNAL_DIFF变量来使用的方法。但是,该变量应该指向可执行文件的完整路径。此外,Git_external_diff指定的可执行文件将使用固定的7个参数调用:
由于大多数DIFF工具都需要不同的顺序(并且只有某些参数),因此您很可能必须指定包装脚本转到真正的差异工具。
我更喜欢的第二种方法是通过“ git配置外部差异工具
config“ 。这是我所做的:
1)创建一个包装脚本“ git-diff-wrapper.sh”,其中包含类似的内容
,只有第二个(“ old-file”)和第五个( “新文件”)论点将是
传递给差异工具。
2)
在命令提示符下键入“ git-diff-wrapper.sh”的路径,因此您的〜/.gitConfig包含
确保使用正确的语法来指定包装脚本和diff的路径
工具,即使用前向斜线而不是后斜线。就我而言,我有
.gitConfig和
包装脚本。介意尾随的“猫”!
(我想仅对于某些可能无法返回适当或一致的返回状态的程序才需要'
| cat
'。如果您的diff工具具有明确的返回状态,则可能需要在没有拖延猫的情况下尝试)( 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:
要完成我以前的 。
/901386#901386“> jakub ,git1.6.3引入 difftool ,最初于2008年9月提出:
usage =
'[ - tool = tool = tool] [ - commit = ref] [-start = ref - ref - end = - end = ref] [ - no-prompt] [file to Merge]'
(请参阅
- 本答案的最后一部分中的ExtCMD
)$ local
包含来自启动修订版中文件的内容,$ $远程
包含末尾修订中文件的内容。$ base
在WOR中包含文件的内容最后一个用例是您想将当前的工作者与其他事物进行比较时比头(例如标签)
注意:由于Git 2.5,
git config diff.tool winmerge
就足够了!请参阅“ git mergetool winmerge ”
和自Git 1.7.11 以来,您拥有选项
- Dir-Diff
,以便为了产生外部DIFF工具,可以在填充两个临时目录后的一次可以比较两个目录层次结构,而不是每个文件对运行一次外部工具的实例。Git 2.5之前:
使用自定义差异工具配置
difftool
的实际情况:使用Winmerge.sh存储在路径的目录部分中:
如果您有另一个工具(KDIFF3,P4DIFF,...),创建另一个shell脚本和适当的
fifftool.mydifftool.cmd
config指令。然后,您可以使用
diff.tool
config轻松切换工具。您还拥有此添加其他详细信息。
(或此问题对于
winmergeu
选项)此设置的兴趣是
winmerge.sh
脚本:您可以将其自定义以考虑特殊情况。请参阅例如 David Marble with-visual-diff-program/4116806#4116806“>下面的答案 有关:
kem Mason a href =“ https://stackoverflow.com/questions/255202/how-do-i-i-i-view-git-diff-diff-dput-with-with-visual-visual-diff-program/4881489#4881489>您还可以使用
- Extcmd
option :避免任何包装器:例如,这就是
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 file:
可选的是:
也请查看 diffall ,我写的一个简单的脚本是为了扩展串行打开每个人的烦人(IMO)默认的差异行为。
Windows上的Global .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有“ git difftool ”,您可以配置以使用自己喜欢的图形差异工具。
目前受支持(在编写此答案时)开箱即用的是 kdiff3 ,, kompare , tkdiff , meld , xxdiff ,Emerge,vimdiff,gvimdiff,ecmerge,和 opendiff ;如果您要使用的工具不在此列表中,则可以始终使用'
difftool。
“ 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
应用
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仅更改一个文件。
创建一个批处理文件以调用您的diff程序,并使用参数2和5。此文件必须在您的路径中。 (如果您不知道那在哪里,请将其放入 c:\ windows 。)例如“ gitdiff.bat”。我的是:
设置环境变量以指向批处理文件。例如:
git_external_diff = gitdiff.bat
。或通过powershell键入git config -global diff.external gitdiff.bat
。不使用引号或指定任何路径信息很重要,否则它行不通。这就是为什么 gitdiff.bat 必须在您的路径中。
现在,当您键入“ git diff”时,它将调用您的外部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:
查看了其他一些外部差异工具后,我发现Intellij Idea(和Android Studio)中的
diff
视图对我来说是最好的。步骤1-如果要使用Intellij Ideas作为diff工具,则应从命令行中运行Intellij Ideas
,请首先设置Intellij Idey以按照指令从命令行运行
或unix:
工具|创建命令行启动器
。对话框创建启动器脚本打开,并带有启动器脚本的建议路径和名称。您可以接受默认值或指定自己的路径。注意它,因为稍后您需要。
在Intellij Idea之外,将启动器脚本的路径和名称添加到您的路径中。
在Windows上:
步骤2-配置git以按照
此博客文章:
bash
fish
现在将以下内容添加到您的git配置:
您可以使用
尝试一下git difftool
或git difftool头〜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&nbsp; 7上有用。无需中介 sh scripts
的内容。 gitconfig:
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.