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

我在我的.gitconfig中有这个:

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

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

export GIT_EXTERNAL_DIFF=git-chdiff

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


当前回答

Git提供了一系列预配置的“开箱即用”的difftools (kdiff3、kompare、tkdiff、meld、xxdiff、emerge、vimdiff、gvimdiff、ecmerge、diffuse、opendiff、p4merge和araxis),还允许您指定自己的difftools。要使用一个预先配置的difftools(例如,“vimdiff”),您可以在~/.gitconfig中添加以下行:

[diff]
    tool = vimdiff

现在,您将能够运行“git difftool”并使用您选择的工具。

另一方面,指定你自己的difftool需要更多的工作,看看我如何用我喜欢的diff工具/查看器查看'git diff'输出?

其他回答

从这个问题中复制我的答案,这个问题更具体地是将Beyond Compare设置为Git的diff工具。我所分享的所有细节在一般情况下对任何diff工具都同样有用,所以我在这里分享。

我们运行的第一个命令如下所示:

git config --global diff.tool bc3

上面的命令在%userprofile%目录下的.gitconfig文件中创建以下条目:

[diff]
    tool = bc3

%userprofile%是一个环境变量,您可以在运行提示符中输入该变量并按Enter键打开包含.gitconfig文件的目录位置。

就是这样。这就是你所需要的,同时设置一个已经发布的Git已经知道的比较工具的版本,就像在这个例子中,Git已经知道Beyond Compare的第三个版本。

现在让我们来深潜吧!

此外,您可能还需要运行以下命令:

git config --global difftool.bc3.path "c:/program files/beyond compare 3/bcomp.exe"

该命令为可选命令。只有在某些特殊情况下才需要它。我们很快就会知道它的原因。

这里要知道的最重要的东西是键bc3。这是Git的一个众所周知的键,它映射到市场上可用的特定比较工具的特定版本,例如,在这种情况下,bc3对应于Beyond Compare工具的第三个版本。如果你想查看Git维护的完整键列表,请在Git Bash命令行上运行以下命令:

git difftool --tool-help

当我们运行上面的命令时,它会返回下面的列表:

vimdiff
vimdiff2
vimdiff3
araxis
bc
bc3
codecompare
deltawalker
diffmerge
diffuse
ecmerge
emerge
examdiff
gvimdiff
gvimdiff2
gvimdiff3
kdiff3
kompare
meld
opendiff
p4merge
tkdiff
winmerge
xxdiff

在为Git设置比较工具时,我们可以根据工具及其版本使用上述任何已存在的键,例如,对于Beyond Compare v1,我们将使用键bc,对于Beyond Compare v3,我们将使用键bc3。

But in some cases, we might have to define a brand new key of our own e.g. let's say we're setting-up a brand new comparison tool which has just been released to market. For obvious reasons the current version of Git installed on your machine will not show any key corresponding to this new tool. Eventually Git will show it in a future release but not immediately. Similarly, this problem can occur for a newly released version of an existing tool also e.g. there is no key for Beyond Compare v4 in above list. So you are always free to map any tool to any of pre-existing keys or to a new custom key of your own.

现在让我们来理解下面的场景,同时设置一个比较工具:

一个旧工具的新版本已经发布,它没有映射到Git中的任何预定义键?

OR

绝对是全新的市场

就像在我的例子中,我安装了Beyond Compare v4。Git已经知道Beyond Compare工具,但是它的版本4没有映射到任何现有的键。所以我们可以采用以下任何一种方法:

Since no key exists in Git for Beyond Compare v4 so we can map it to the already existing key bc3 even though it is meant to be mapped to Beyond Compare v3. We can outsmart Git to save some effort. Now here is the answer to the question we left unanswered in the first paragraph - If you map any tool to the key which is already known to Git then you would not need to run the second command. This is because the tool's EXE location is already known to Git. But remember, this will work only when the EXE location of the tool doesn't change across versions. If Beyond Compare v3 and v4 have different install locations in %programfiles% directory then it becomes mandatory to run the second command. For e.g. if I had installed Beyond Compare v3 on my box then having below configuration in my .gitconfig file would have been sufficient to complete the setup process. This is based on the assumption that v3 and v4 will have same install path. [diff] tool = bc3 But if we want to associate the non-default tool then we need to mention the path attribute separately so that Git will know the path of EXE from where it has to be launched. Below entry tells Git to launch Beyond Compare v4 instead. Note the EXE's path: [difftool "bc3"] path = c:/program files/Beyond Compare 4/bcomp.exe Also, if we wanted we could have mapped Beyond Compare v4 to any of the pre-defined keys of other tools as well e.g. examdiff. Git won't stop you from doing this bad thing. Although we should not do so avoid a maintenance nightmare. Cleanest approach is to define a custom key. We can define a brand new key for any new comparison tool or a new version of an old tool. Like in my case I defined a new key bc4 as it is fairly intuitive. I could have named it foobaar. Now when the key is absolutely new, the setup process is slightly different. In this case you have to run two commands in all. But our second command will not be setting path of our new tool's EXE. Instead we have to set cmd attribute for our new tool as shown below: git config --global diff.tool bc4 git config --global difftool.bc4.cmd "\"C:\\Program Files\\Beyond Compare 4\\bcomp.exe\" -s \"\$LOCAL\" -d \"\$REMOTE\"" Running above commands creates below entries in your .gitconfig file: [diff] tool = bc4 [difftool "bc4"] cmd = \"C:\\Program Files\\Beyond Compare 4\\bcomp.exe\" -s \"$LOCAL\" -d \"$REMOTE\"

我强烈建议您遵循方法# 2,以避免将来出现任何维护问题。

添加下面的一个块可以让我在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

参考微软的VS代码技巧和技巧。 在你的终端上运行这些命令:

Git配置全局合并。工具代码

但是首先需要将code命令添加到PATH环境变量中。

在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++中打开提交信息。

其他人已经给出了99%的答案,但还有一个步骤被遗漏了。(我的答案将来自OS X,所以你必须相应地改变文件路径。)

对~/.gitconfig进行以下更改:

[diff]
    tool = diffmerge
[difftool "diffmerge"]
    cmd = /Applications/Diffmerge.app/Contents/MacOS/diffmerge $LOCAL $REMOTE

这将修复差异工具。您也可以在不编辑~/的情况下修复此问题。通过从终端输入这些命令,直接打开Gitconfig:

git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd "/Applications/DiffMerge.appContents/MacOS/diffmerge \$LOCAL \$REMOTE"

其他人没有提到的1%是,当使用这个时,你不能只运行git diff myfile.txt;您需要运行git difftool myfile.txt。