如何查看“git diff” 使用我喜欢的差异工具/查看器输出?

发布于 2024-07-07 12:08:57 字数 100 浏览 14 评论 0原文

当我输入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(26

寂寞花火° 2024-07-14 12:08:57

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


可能是这样 文章会对您有所帮助。 以下是最好的部分:

有两种不同的方法来指定外部差异工具。

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

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

由于大多数 diff 工具将需要不同的参数顺序(并且仅需要一些),因此您很可能必须指定一个包装器脚本,该脚本在Turn 调用真正的 diff 工具。

我更喜欢的第二种方法是通过“git”配置外部 diff 工具
这是我所做的:

1)创建一个包装脚本“git-diff-wrapper.sh”,其中包含类似的内容

-->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)--

如您所见,只有第二个(“旧文件”)和第五个( “新文件”)参数将是
传递给 diff 工具。

2)

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

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

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

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

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

在 .gitconfig 和

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

包装脚本中都有。 注意后面的“猫”!

(我认为只有某些可能无法返回正确或一致的返回状态的程序才需要“| cat”。如果您的 diff 工具具有明确的返回状态,您可能需要尝试不使用尾随的 cat )

Diomidis Spinellis 添加了在评论中

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


(上面引用的文章)是外部工具通过配置文件定义的理论(而不是通过环境变量)。
实际中(还是外部工具的配置文件定义),可以参考:

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:

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.

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

-->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) Type

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

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\"

in .gitconfig and

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

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:

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:

假情假意假温柔 2024-07-14 12:08:57

完成我之前的 "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 包含文件中的内容

它基本上是对 git-mergetool 进行修改以在 git 索引/工作树上进行操作。

此脚本的通常用例是当您有暂存或未暂存的更改,并且您希望在并排差异查看器中查看更改时(例如 xxdifftkdiff 等)。

git difftool [<filename>*]

另一个用例是当您想要查看相同的信息但比较任意提交时(这是 revarg 解析可能更好的部分)

git difftool --start=HEAD^ --end=HEAD [-- <filename>*]

最后一个用例是当您想将当前工作树与其他工作树进行比较时比 HEAD (例如标签)

git difftool --commit=v1.0.0 [-- <filename>*]

注意:从 Git 2.5 开始,git config diff.tool winmerge 就足够了!
请参阅“git mergetool winmerge

从 Git 1.7.11 开始,您可以选择 --dir-diff,以便生成外部 diff 工具,该工具可以在填充两个临时目录后一次比较两个目录层次结构,而不是每个文件对运行一次外部工具的实例。


Git 2.5 之前:

使用自定义 diff 工具配置 difftool 的实际案例:

C:\myGitRepo>git config --global diff.tool winmerge
C:\myGitRepo>git config --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
C:\myGitRepo>git config --global difftool.prompt false

将 winmerge.sh 存储在 PATH 的目录部分中:

#!/bin/sh
echo Launching WinMergeU.exe: $1 $2
"C:/Program Files/WinMerge/WinMergeU.exe" -u -e "$1" "$2" -dl "Local" -dr "Remote"

如果您有其他工具(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 选项避免使用任何包装器

--extcmd=<command>

指定用于查看差异的自定义命令。 指定此选项后,git-difftool 将忽略配置的默认值并运行 $command $LOCAL $REMOTE

例如,这就是 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 wor

It's basically git-mergetool modified to operate on the git index/worktree.

The usual use case for this script is when you have either staged or unstaged changes and you'd like to see the changes in a side-by-side diff viewer (e.g. xxdiff, tkdiff, etc).

git difftool [<filename>*]

Another use case is when you'd like to see the same information but are comparing arbitrary commits (this is the part where the revarg parsing could be better)

git difftool --start=HEAD^ --end=HEAD [-- <filename>*]

The last use case is when you'd like to compare your current worktree to something other than HEAD (e.g. a tag)

git difftool --commit=v1.0.0 [-- <filename>*]

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:

C:\myGitRepo>git config --global diff.tool winmerge
C:\myGitRepo>git config --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
C:\myGitRepo>git config --global difftool.prompt false

With winmerge.sh stored in a directory part of your PATH:

#!/bin/sh
echo Launching WinMergeU.exe: $1 $2
"C:/Program Files/WinMerge/WinMergeU.exe" -u -e "$1" "$2" -dl "Local" -dr "Remote"

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.shscript: you can customize it to take into account special cases.

See for instance David Marble's answer below for an example which deals with:

  • new files in either origin or destination
  • removed files in either origin or destination

As Kem Mason mentions in his answer, you can also avoid any wrapper by using the --extcmd option:

--extcmd=<command>

Specify a custom command for viewing diffs. git-difftool ignores the configured defaults and runs $command $LOCAL $REMOTE when this option is specified.

For instance, this is how gitk is able to run/use any diff tool.

假面具 2024-07-14 12:08:57

尝试这个解决方案:

$ meld my_project_using_git

Meld 理解 Git 并提供围绕最近更改的导航。

Try this solution:

$ meld my_project_using_git

Meld understands Git and provides navigating around the recent changes.

第七度阳光i 2024-07-14 12:08:57

使用新的 git difftool,只需将其添加到 .gitconfig 文件中即可:(

[diff]
    tool = any-name
[difftool "any-name"]
    cmd = "\"C:/path/to/my/ext/diff.exe\" \"$LOCAL\" \"$REMOTE\""

可选)还可以添加:

[difftool]
    prompt = false

另请查看 diffall ,我编写的一个简单脚本,用于扩展串行打开每个文件的烦人的(IMO)默认差异行为。

Windows 上的全局 .gitconfig 位于 %USERPROFILE%\.gitconfig

With new git difftool, its as simple as adding this to your .gitconfig file:

[diff]
    tool = any-name
[difftool "any-name"]
    cmd = "\"C:/path/to/my/ext/diff.exe\" \"$LOCAL\" \"$REMOTE\""

Optionally, also add:

[difftool]
    prompt = false

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

笨笨の傻瓜 2024-07-14 12:08:57

从 Git 版本 1.6.3 开始,有“

目前(在撰写本答案时)开箱即用的支持有KDiff3KomparetkdiffMeldxxdiff、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".

葮薆情 2024-07-14 12:08:57

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

git difftool -t

我也喜欢让默认的 diff 只是常规命令行,因此设置GIT_EXTERNAL_DIFF 变量不是一个选项。

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

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

它只是将 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

git difftool -t

I also like to have the default diff just be the regular command line, so setting the GIT_EXTERNAL_DIFF variable isn't an option.

You can use an arbitrary diff app as a one-off with this command:

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

It just passes the 2 files to the command you specify, so you probably don't need a wrapper either.

¢蛋碎的人ぎ生 2024-07-14 12:08:57

构建于 VonC 的答案 要处理文件删除和添加,请使用以下命令和脚本:

git config --global diff.tool winmerge
git config --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\" \"$BASE\""
git config --global difftool.prompt false

这与将其放入全局文件.gitconfig相同:

[diff]
    tool = winmerge
[difftool "winmerge"]
    cmd = winmerge.bat "$LOCAL" "$REMOTE" "$BASE"
[difftool]
    prompt = false

然后将以下内容放入文件 >winmerge.sh 必须位于您的路径上:

#!/bin/sh
NULL="/dev/null"
if [ "$2" = "$NULL" ] ; then
    echo "removed: $3"
elif [ "$1" = "$NULL" ] ; then
    echo "added: $3"
else
    echo "changed: $3"
    "C:/Program Files (x86)/WinMerge/WinMergeU.exe" -e -ub -dl "Base" -dr "Mine" "$1" "$2"
fi

Building on VonC's answer to deal with file removals and additions, use the following commands and scripts:

git config --global diff.tool winmerge
git config --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\" \"$BASE\""
git config --global difftool.prompt false

Which is the same as putting this in your global file .gitconfig:

[diff]
    tool = winmerge
[difftool "winmerge"]
    cmd = winmerge.bat "$LOCAL" "$REMOTE" "$BASE"
[difftool]
    prompt = false

Then put the following in file winmerge.sh which must be on your path:

#!/bin/sh
NULL="/dev/null"
if [ "$2" = "$NULL" ] ; then
    echo "removed: $3"
elif [ "$1" = "$NULL" ] ; then
    echo "added: $3"
else
    echo "changed: $3"
    "C:/Program Files (x86)/WinMerge/WinMergeU.exe" -e -ub -dl "Base" -dr "Mine" "$1" "$2"
fi
极度宠爱 2024-07-14 12:08:57

Windows/MSYS Git 的解决方案

阅读答案后,我发现了一种更简单的方法,涉及只更改一个文件。

  1. 使用参数 2 和 5 创建一个批处理文件来调用 diff 程序。该文件必须位于您的路径中的某个位置。 (如果您不知道它在哪里,请将其放在C:\windows中。)例如,将其命名为“gitdiff.bat”。 我的是:

    @echo 关闭 
      REM 这是 gitdiff.bat 
      “C:\Program Files\WinMerge\WinMergeU.exe”%2 %5 
      
  2. 设置环境变量以指向您的批处理文件。 例如: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.

  1. 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:

    @echo off
    REM This is gitdiff.bat
    "C:\Program Files\WinMerge\WinMergeU.exe" %2 %5
    
  2. Set the environment variable to point to your batch file. For example:GIT_EXTERNAL_DIFF=gitdiff.bat. Or through PowerShell by typing git 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.

御守 2024-07-14 12:08:57

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

If you're doing this through Cygwin, you may need to use 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`
放我走吧 2024-07-14 12:08:57

在查看了其他一些外部 diff 工具后,我发现 IntelliJ IDEA(和 Android Studio)中的 diff 视图对我来说是最好的。

步骤 1 - 设置 IntelliJ IDEA 从命令行运行

如果您想使用 IntelliJ IDEA 作为 diff 工具,您应该首先按照说明设置 IntelliJ IDEA 从命令行运行 此处

在 macOS 上或 UNIX:

  1. 确保 IntelliJ IDEA 正在运行。
  2. 在主菜单上,选择“工具”|“工具”。 创建命令行启动器。 将打开“创建启动器脚本”对话框,其中包含启动器脚本的建议路径和名称。 您可以接受默认路径,也可以指定您自己的路径。
    请注意它,因为稍后您将需要它。
    在 IntelliJ IDEA 外部,将启动器脚本的路径和名称添加到您的路径中。

在 Windows 上:

  1. 在 Path 系统环境变量中指定 IntelliJ IDEA 可执行文件的位置。 在这种情况下,您将能够从任何目录调用 IntelliJ IDEA 可执行文件和其他 IntelliJ IDEA 命令。

步骤 2 - 配置 git 以使用 IntelliJ IDEA 作为 diff 工具

按照 这篇博文

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")

您可以使用 尝试一下git difftoolgit 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:

  1. Make sure IntelliJ IDEA is running.
  2. On the main menu, choose 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:

  1. Specify the location of the IntelliJ IDEA executable in the Path system environment variable. In this case, you will be able to invoke the IntelliJ IDEA executable and other IntelliJ IDEA commands from any directory.

Step 2 - configure git to use IntelliJ IDEA as the difftool

Following the instructions on this blog post:

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

Now add the following to your git config:

[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")

You can try it out with git difftool or git difftool HEAD~1

喜爱皱眉﹌ 2024-07-14 12:08:57

之前的精彩答案的简短摘要:

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

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

git difftool

A short summary of the previous great answers:

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

Then use it by typing (optionally specifying the file name as well):

git difftool
聚集的泪 2024-07-14 12:08:57

这对我在 Windows 7 上有效。 不需要任何中间 sh 脚本

的内容。 git配置:

    [diff]
      tool = kdiff3

    [difftool]
       prompt = false

    [difftool "kdiff3"]
      path = C:/Program Files (x86)/KDiff3/kdiff3.exe
      cmd = "$LOCAL" "$REMOTE"

This works for me on Windows 7. There isn't any need for intermediary sh scripts

Contents of .gitconfig:

    [diff]
      tool = kdiff3

    [difftool]
       prompt = false

    [difftool "kdiff3"]
      path = C:/Program Files (x86)/KDiff3/kdiff3.exe
      cmd = "$LOCAL" "$REMOTE"
飘落散花 2024-07-14 12:08:57

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

Install Meld:

 # apt-get install meld

Then choose that as the difftool:

 $ git config --global diff.tool meld

If you want to run it in the console, type:

 $ git difftool

If you want to use graphic mode, type:

 $ git mergetool

And the output would be:

 '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):

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.

梦冥 2024-07-14 12:08:57

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

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 --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
绿光 2024-07-14 12:08:57

如果您使用的是 Mac 并且有 Xcode,那么您就有 FileMerge 已安装。 终端命令是 opendiff,所以你可以这样做:

git difftool -t 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:

git difftool -t opendiff
怀里藏娇 2024-07-14 12:08:57

简介

作为参考,我想在 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)。 两个命令都采用相同的参数,因此例如要直观地比较特定文件中的更改,您可以键入

gitdiff path/file.txt

Setup

请注意,$GitInstall 用作安装 Git 的目录的占位符。

  1. 创建一个新文件,$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 差异%*
    终端本地

  2. 创建一个新文件 $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

  3. 现在你已经完成了。 从 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 redirects git 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 type

gitdiff path/file.txt

Setup

Note that $GitInstall is used as a placeholder for the directory where Git is installed.

  1. Create a new file, $GitInstall\cmd\gitdiff.cmd

     @echo off
     setlocal
     for /F "delims=" %%I in ("%~dp0..") do @set path=%%~fI\bin;%%~fI\mingw\bin;%PATH%
     if "%HOME%"=="" @set HOME=%USERPROFILE%
     set GIT_EXTERNAL_DIFF=git-diff-visual.cmd
     set GIT_PAGER=cat
     git diff %*
     endlocal
    
  2. 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)

     @echo off
     rem diff is called by git with 7 parameters:
     rem path old-file old-hex old-mode new-file new-hex new-mode
     echo Diffing "%5"
     "[visual_diff_exe]" "%2" "%5"
     exit 0
    
  3. You're now done. Running gitdiff from within a Git repository should now invoke your visual diff program for every file that was changed.

初与友歌 2024-07-14 12:08:57

有关如何在 1.6.3 之前的 Git 版本上配置 diff 工具的 Linux 版本(1.6.3 将 difftool 添加到 Git),是一个很棒的简洁教程。

简而言之:

第 1 步:将其添加到您的 .gitconfig

[diff]
  external = git_diff_wrapper
[pager]
  diff =

第 2 步:创建一个名为 git_diff_wrapper 的文件,将其放在 $PATH 中的某个位置

#!/bin/sh

vimdiff "$2" "$5"

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

[diff]
  external = git_diff_wrapper
[pager]
  diff =

Step 2: create a file named git_diff_wrapper, put it somewhere in your $PATH

#!/bin/sh

vimdiff "$2" "$5"
无声静候 2024-07-14 12:08:57

在 Mac OS X 上,

git difftool -t diffuse

在 Git 文件夹中为我完成这项工作。 要安装 Diffuse,可以使用端口 -

sudo port install diffuse

On Mac OS X,

git difftool -t diffuse

does the job for me in the Git folder. For installing Diffuse, one can use port -

sudo port install diffuse
谁的年少不轻狂 2024-07-14 12:08:57

以下内容可以从这里的其他答案中收集到,但对我来说这很困难(信息太多),所以这里是 tkdiff 的“只需输入”答案:

git difftool --tool=tkdiff <path to the file to be diffed>

您可以用您最喜欢的比较工具的可执行名称替换 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:

git difftool --tool=tkdiff <path to the file to be diffed>

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.

唐婉 2024-07-14 12:08:57

您可以使用 git difftool 。

例如,如果您有 Meld,则可以编辑分支 master开发者:

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

You can use git difftool.

For example, if you have Meld, you can edit the branches master and devel by:

git config --global diff.external meld
git difftool master..devel
甜宝宝 2024-07-14 12:08:57

我在这里尝试了一些奇特的东西(使用 tkdiff),但没有任何效果。 所以我写了下面的脚本,tkgitdiff。 它做了我需要它做的事情。

$ cat tkgitdiff
#!/bin/sh

#
# tkdiff for git.
# Gives you the diff between HEAD and the current state of your file.
#

newfile=$1
git diff HEAD -- $newfile > /tmp/patch.dat
cp $newfile /tmp
savedPWD=$PWD
cd /tmp
patch -R $newfile < patch.dat
cd $savedPWD
tkdiff /tmp/$newfile $newfile

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.

$ cat tkgitdiff
#!/bin/sh

#
# tkdiff for git.
# Gives you the diff between HEAD and the current state of your file.
#

newfile=$1
git diff HEAD -- $newfile > /tmp/patch.dat
cp $newfile /tmp
savedPWD=$PWD
cd /tmp
patch -R $newfile < patch.dat
cd $savedPWD
tkdiff /tmp/$newfile $newfile
愁以何悠 2024-07-14 12:08:57

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

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

使用 git-meld

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

但现在我厌倦了总是使用 Meld 在图形环境中,使用此设置调用正常差异并不简单,因此我切换到此设置:

[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

我仍然可以保留旧的 git diff

I've been using this bit in file ~/.gitconfig for a long time:

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

With git-meld:

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

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:

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

With this setup, things like this work:

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

And I still get to keep the good old git diff.

红玫瑰 2024-07-14 12:08:57

我在 Ubuntu 上使用 Kompare

sudo apt-get install kompare

比较两个分支:

git difftool -t kompare <my_branch> master

I use Kompare on Ubuntu:

sudo apt-get install kompare

To compare two branches:

git difftool -t kompare <my_branch> master
说不完的你爱 2024-07-14 12:08:57

如果您碰巧已经有一个与文件类型关联的 diff 工具(例如,因为您安装了带有 diff 查看器的 TortoiseSVN),您可以将常规的 git diff 输出通过管道传输到“临时”文件,然后只需直接打开该文件,无需了解有关查看器的任何信息:

git diff > "~/temp.diff" && start "~/temp.diff"

将其设置为全局别名效果更好:git What

[alias]
    what = "!f() { git diff > "~/temp.diff" && start "~/temp.diff"; }; f"

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:

git diff > "~/temp.diff" && start "~/temp.diff"

Setting it as a global alias works even better: git what

[alias]
    what = "!f() { git diff > "~/temp.diff" && start "~/temp.diff"; }; f"
×纯※雪 2024-07-14 12:08:57

如果您不熟悉命令行,那么如果您安装 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.

等风来 2024-07-14 12:08:57

您可能想尝试 xd,它是 Git/SVN diff 的 GUI 包装器。 它本身不是一个差异工具。

当您想要运行 git diffsvn diff 时,您运行 xd ,它会向您显示文件列表、预览窗口,您可以启动您喜欢的任何差异工具,包括 tkdiff、xxdiff、gvimdiff、Emacs (ediff)、XEmacs (ediff),Meld漫反射KompareKDiff3。 您还可以运行任何自定义工具。

不幸的是,该工具不支持 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 run git diff or svn 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文