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

我在我的.gitconfig中有这个:

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

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

export GIT_EXTERNAL_DIFF=git-chdiff

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


当前回答

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

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

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

其他回答

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

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

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

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

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

从这个问题中复制我的答案,这个问题更具体地是将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,以避免将来出现任何维护问题。

这是我的~/的一部分。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命令。否则,我需要给出可执行文件的完整路径。