如何使用我喜欢的差异工具/查看器查看“git diff"输出? [英] How do I view 'git diff' output with my preferred diff tool/ viewer?

查看:37
本文介绍了如何使用我喜欢的差异工具/查看器查看“git diff"输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入 git diff 时,我想使用我选择的可视化差异工具(Windows 上的 SourceGeardiffmerge")查看输出.我如何配置 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?

推荐答案

从Git1.6.3开始,可以使用git difftool脚本:见我在下面的回答.

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.

第一个是您使用的方法,通过设置 GIT_EXTERNAL_DIFF 变量.但是,该变量应该指向可执行文件的完整路径.此外,将使用一组固定的 7 个参数调用由 GIT_EXTERNAL_DIFF 指定的可执行文件:

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:

path old-file old-hex old-mode new-file new-hex new-mode

由于大多数差异工具将需要不同顺序(且仅部分)的参数,因此您很可能必须指定一个包装脚本,它反过来调用真正的差异工具.

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.

我更喜欢的第二种方法是通过gitconfig".这是我所做的:

The second method, which I prefer, is to configure the external diff tool via "git config". Here is what I did:

1) 创建一个包含类似内容的包装脚本git-diff-wrapper.sh"

1) Create a wrapper script "git-diff-wrapper.sh" which contains something like

-->8-(snip)--
#!/bin/sh

# diff is called by git with 7 parameters:
# path old-file old-hex old-mode new-file new-hex new-mode

"<path_to_diff_executable>" "$2" "$5" | cat
--8<-(snap)--

如您所见,只有第二个(旧文件")和第五个(新文件")参数是传递给差异工具.

As you can see, only the second ("old-file") and fifth ("new-file") arguments will be passed to the diff tool.

2) 输入

$ git config --global diff.external <path_to_wrapper_script>

在命令提示符下,替换为git-diff-wrapper.sh"的路径,因此您的 ~/.gitconfig 包含

at the command prompt, replacing with the path to "git-diff-wrapper.sh", so your ~/.gitconfig contains

-->8-(snip)--
[diff]
    external = <path_to_wrapper_script>
--8<-(snap)--

确保使用正确的语法来指定包装器脚本和差异的路径工具,即使用正斜杠而不是反斜杠.就我而言,我有

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

[diff]
    external = "c:/Documents and Settings/sschuber/git-diff-wrapper.sh"

在 .gitconfig 和

in .gitconfig and

"d:/Program Files/Beyond Compare 3/BCompare.exe" "$2" "$5" | cat

在包装脚本中.注意尾随的猫"!

in the wrapper script. Mind the trailing "cat"!

(我认为 '| cat' 仅适用于某些可能无法返回正确或一致返回状态的程序.如果您的 diff 工具具有明确的返回状态)

(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 增加了 在评论中:

cat 命令是必需的,因为 diff(1),如果文件不同,默认情况下会以错误代码退出.
Git 期望外部 diff 程序仅在发生实际错误时才以错误代码退出,例如如果内存不足.
通过将 git 的输出传送到 cat,非零错误代码被屏蔽.
更有效的是,程序可以运行 exit 并且参数为 0.)

The cat command is required, because diff(1), by default exits with an error code if the files differ.
Git expects the external diff program to exit with an error code only if an actual error occurred, e.g. if it run out of memory.
By piping the output of git to cat the non-zero error code is masked.
More efficiently, the program could just run exit with and argument of 0.)

<小时>

那(上面引用的文章)是外部工具的理论通过配置文件定义(不是通过环境变量).
在实践中(仍然是外部工具的配置文件定义),可以参考:


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:

  • How do I setup DiffMerge with msysgit / gitk? which illustrates the concrete settings of DiffMerge and WinMerge for MsysGit and gitk
  • How can I set up an editor to work with Git on Windows? for the definition of Notepad++ as an external editor.

这篇关于如何使用我喜欢的差异工具/查看器查看“git diff"输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆