如何配置Git以使用不同的工具来区分.gitconfig文件?

我在我的.gitconfig中有这个:

[diff]
    tool = git-chdiff #also tried /bin/git-chdiff

它不起作用;它只是打开常规命令行差异。当我这样做

export GIT_EXTERNAL_DIFF=git-chdiff

然后git diff将打开外部差异工具(所以我知道外部差异工具脚本工作良好)。我对diff工具的.gitconfig配置有问题吗?


当前回答

这是我的~/的一部分。gitconfig,我配置差异和合并工具。我喜欢SourceGear的diffmerge。(事实上,我非常非常喜欢它)。

[merge]
        tool = diffmerge
[mergetool "diffmerge"]
        cmd = "diffmerge --merge --result=\"$MERGED\" \"$LOCAL\" \"$(if test -f \"$BASE\"; then echo \"$BASE\"; else echo \"$LOCAL\"; fi)\" \"$REMOTE\""
        trustExitCode = false
[diff]
        tool = diffmerge
[difftool "diffmerge"]
        cmd = diffmerge \"$LOCAL\" \"$REMOTE\"

你看,你在[difftool "diffmerge"]行中定义了一个名为"diffmerge"的工具。然后我在[diff] tool =部分设置工具“diffmerge”为默认值。

很明显,我的路径中有diffmerge命令。否则,我需要给出可执行文件的完整路径。

其他回答

如果你想有一个选项来使用多个diff工具,添加一个别名文件。gitconfig:

[alias]
    kdiff = difftool --tool kdiff3

另一种方法(从命令行):

git config --global diff.tool tkdiff
git config --global merge.tool tkdiff
git config --global --add difftool.prompt false

前两行将difftool和mergetool设置为tkdiff—根据您的首选项进行更改。第三行禁用了恼人的提示,因此无论何时点击git difftool,它都会自动启动difftool。

添加下面的一个块可以让我在Windows和Linux开发环境中使用KDiff3。这是一个很好的一致的跨平台差异和合并工具。

Linux

[difftool "kdiff3"]
    path = /usr/bin/kdiff3
    trustExitCode = false
[difftool]
    prompt = false
[diff]
    tool = kdiff3
[mergetool "kdiff3"]
    path = /usr/bin/kdiff3
    trustExitCode = false
[mergetool]
    keepBackup = false
[merge]
    tool = kdiff3

窗户

[difftool "kdiff3"]
    path = C:/Progra~1/KDiff3/kdiff3.exe
    trustExitCode = false
[difftool]
    prompt = false
[diff]
    tool = kdiff3
[mergetool "kdiff3"]
    path = C:/Progra~1/KDiff3/kdiff3.exe
    trustExitCode = false
[mergetool]
    keepBackup = false
[merge]
    tool = kdiff3

在前面的回答中,几乎所有的解决方案都不能在Git版本2中使用

我的:Git版本= 2.28.0

difftool的解决方案:git配置——global diff.tool vimdiff

有了它,你就可以毫无问题地使用它了。

在Windows中,我们需要运行git difftool——tool-help命令来查看各种选项,例如:

    'git difftool --tool=<tool>' may be set to one of the following:
                    vimdiff
                    vimdiff2
                    vimdiff3

    The following tools are valid, but not currently available:
                    araxis
                    bc
                    bc3
                    codecompare
                    deltawalker
                    diffmerge
                    diffuse
                    ecmerge
                    emerge
                    examdiff
                    gvimdiff
                    gvimdiff2
                    gvimdiff3
                    kdiff3
                    kompare
                    meld
                    opendiff
                    p4merge
                    tkdiff
                    winmerge
                    xxdiff

Some of the tools listed above only work in a windowed
environment. If run in a terminal-only session, they will fail.

我们可以添加它们中的任何一个(例如,WinMerge)

git difftool --tool=winmerge

将notepad++配置为在提交前查看文件:

 git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

使用git commit将在notepad++中打开提交信息。