简体   繁体   English

默认的 git diff 工具(和合并工具)是什么?

[英]What is the default git diff tool(and merge tool)?

What is the default git diff tool(and merge tool)?默认的 git diff 工具(和合并工具)是什么?
Where(and how) can I find it out?我在哪里(以及如何)找到它?

I've never set any configuration for git difftool(nor mergetool) explicitly,我从来没有明确地为 git difftool(也不是 mergetool)设置任何配置,
so git config --get difftool shows nothing.所以git config --get difftool什么也没显示。

The git documentation says( https://git-scm.com/docs/git-difftool ): git 文档说( https://git-scm.com/docs/git-difftool ):

If the configuration variable diff.tool is not set, git difftool will pick a suitable default.如果未设置配置变量diff.toolgit difftool将选择合适的默认值。

How can I find out which one it has picked?我怎样才能知道它选择了哪一个?
How does the algorithm for ' suitable ' work? 合适”的算法是如何工作的?


Let me share the reason why I'm trying to find out the currently picked diff tool by my git:让我分享一下我试图通过我的 git 找出当前选择的 diff 工具的原因:
I've met some weird diff result when I execute git diff (I suspect BOM handling issue).当我执行git diff (我怀疑 BOM 处理问题)时,我遇到了一些奇怪的 diff 结果。
I'd like to question the vendor(eg, p4merge ) about it,我想询问供应商(例如p4merge ),
but not sure if it is p4merge , vimdiff or anything else.但不确定它是p4mergevimdiff还是其他任何东西。

I expect there might be a command like git difftool --current .我希望可能会有像git difftool --current这样的命令。

git difftool will tell you what it's going to try. git difftool会告诉你它要尝试什么。

$ git difftool

This message is displayed because 'diff.tool' is not configured.
See 'git difftool --tool-help' or 'git help config' for more details.
'git difftool' will now attempt to use one of the following tools:
kompare emerge vimdiff

Viewing (1/1): 'this'
Launch 'emerge' [Y/n]?

We can find the process in the guess_merge_tool function .我们可以在guess_merge_tool函数中找到这个过程。

guess_merge_tool () {
    list_merge_tool_candidates
    cat >&2 <<-EOF
    This message is displayed because '$TOOL_MODE.tool' is not configured.
    See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
    'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
    $tools
    EOF

    # Loop over each candidate and stop when a valid merge tool is found.
    IFS=' '
    for tool in $tools
    do
        is_available "$tool" && echo "$tool" && return 0
    done

list_merge_tool_candidates sets up the list of $tools . list_merge_tool_candidates设置$tools的列表。 It assumes that if DISPLAY is not set you do not have a GUI which is incorrect on MacOS.它假定如果未设置 DISPLAY,则您没有在 MacOS 上不正确的 GUI。

Then it simply loops through them and picks the first one it finds an executable for using type .然后它简单地循环遍历它们并选择第一个它找到用于使用type的可执行文件。


UPDATE更新

I've met some weird diff result when I execute git diff(I suspect BOM handling issue).我在执行 git diff 时遇到了一些奇怪的 diff 结果(我怀疑是 BOM 处理问题)。 I'd like to question the vendor(eg, p4merge) about it, but not sure if it is p4merge, vimdiff or anything else.我想询问供应商(例如 p4merge),但不确定它是 p4merge、vimdiff 还是其他任何东西。

If you're having an issue with git diff that is with git diff not git difftool .如果您遇到git diff问题,那就是git diff而不是git difftool I think there's some confusion about what git difftool does, so here's a quick overview.我认为git difftool的作用有些混乱,所以这里有一个快速概述。

git diff does not use git-difftool . git diff不使用 git-difftool git difftool does not pick the diff tool for git diff . git difftool不会为git diff选择 diff 工具。 git diff has its own internal diff implementation. git diff有自己的内部差异实现。 It can also use an external diff program, like GNU diff, by supplying --ext-diff .它还可以通过提供--ext-diff来使用外部 diff 程序,如 GNU --ext-diff

When you run git difftool it picks an external diff program and runs it with three environment variables: $LOCAL, $REMOTE, and $MERGED.当您运行git difftool它会选择一个外部 diff 程序并使用三个环境变量运行它:$LOCAL、$REMOTE 和 $MERGED。 $LOCAL is a path to the old version of the file, $REMOTE to the new, and $MERGED to the name of the file so it can be displayed. $LOCAL 是旧版本文件的路径,$REMOTE 是新版本的路径,$MERGED 是文件名以便可以显示。 That's about it.就是这样。 It has no relation to git diff .它与git diff没有关系。

We can see what git difftool does by adding to custom difftools to .gitconfig:我们可以通过将自定义 difftools 添加到 .gitconfig 来查看git difftool作用:

[difftool "echo"]
    cmd = echo "LOCAL: $LOCAL, REMOTE: $REMOTE, MERGED: $MERGED"

[difftool "less"]
    cmd = less "$LOCAL" "$REMOTE"

git difftool -t echo will show the environment variables. git difftool -t echo将显示环境变量。 git difftool -t less will look at the contents of the old and new versions of the files in the less pager. git difftool -t less将查看less分页器中文件的旧版本和新版本的内容。


If you're having a problem with git diff , git difftool has nothing to do with it.如果您在使用git diff遇到问题,则git difftool与它无关。 Nor should p4merge nor vimdiff. p4merge 和 vimdiff 也不应该。

I'm going to add an additional remark that is not mentioned in Schwern's answer.我将添加 Schwern 的回答中未提及的附加说明。

"git difftool" uses the default git difftool if your local Git repository is in the "conflict detected state".如果您的本地 Git 存储库处于“冲突检测状态”,“git difftool”将使用默认的 git difftool。 In other words, in that state, "git difftool" works exactly like "git diff".换句话说,在 state 中,“git difftool”的工作方式与“git diff”完全相同。 I'm not sure if this is a Git bug, or if that's the intended way it's supposed to work.我不确定这是否是一个 Git 错误,或者这是否是它应该工作的预期方式。 So, when, you're in the "conflict detected state", the git difftool configuration is ignored.因此,当您处于“检测到冲突状态”时,git difftool 配置将被忽略。 That's the behavior I witnessed in both version 2.37.x or 2.38.x of "Git for Window".这是我在“Git for Window”的 2.37.x 或 2.38.x 版本中看到的行为。 Once you resolve the git conflict, "git difftool" goes back to using the configured difftool.解决 git 冲突后,“git difftool”将返回使用已配置的 difftool。

Here's an example of what you'll see when your local git repository is put in the "conflict detected state".以下是当您的本地 git 存储库处于“检测到冲突状态”时您将看到的示例。

C:\Temp\user2\core>git pull remote: Enumerating objects: 58, done. remote: Counting objects: 100% (58/58), done. remote: Compressing objects: 100% (32/32), done. remote: Total 40 (delta 12), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (40/40), 3.13 KiB | 46.00 KiB/s, done. From c:\Temp\clone\core a7b9b22..6cce36f main -> origin/main Auto-merging CoreInterfaces/src/main/java/com/phinneyridge/core/Core.java CONFLICT (content): Merge conflict in CoreInterfaces/src/main/java/com/phinneyridge/core/Core.java Automatic merge failed; fix conflicts and then commit the result.

I'm guessing you would not see Schwern's debugging configuration suggestion work if you try it when you're in the conflict state.我猜如果您在冲突 state 中尝试使用 Schwern 的调试配置建议,您将看不到它。

I've summited a bug to the Git community on this issue and we'll find out if they think that behavior is a bug or it's something that works as expected.我已经就此问题向 Git 社区提交了一个错误,我们会查明他们是否认为该行为是错误或它是否按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM