git 使用 .gitconfig 配置差异工具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6412516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:13:17  来源:igfitidea点击:

Configuring diff tool with .gitconfig

git

提问by ryanzec

How do I configure Git to use a different tool for diffing with the .gitconfig file?

如何配置 Git 以使用不同的工具与 .gitconfig 文件进行比较?

I have this in my .gitconfig:

我的 .gitconfig 中有这个:

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

It does not work; it just opens the regular command line diff. When I do

这是行不通的; 它只是打开常规命令行差异。当我做

export GIT_EXTERNAL_DIFF=git-chdiff

then git diffwill open up the external diffing tool (so I know the external diff tool script works fine). Do I have something wrong with my .gitconfig configuration for the diff tool?

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

采纳答案by Fredrik Pihl

Git offers a range of difftools pre-configured "out-of-the-box" (kdiff3, kompare, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, diffuse, opendiff, p4merge and araxis), and also allows you to specify your own. To use one of the pre-configured difftools (for example, "vimdiff"), you add the following lines to your ~/.gitconfig:

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

[diff]
    tool = vimdiff

Now, you will be able to run "git difftool" and use your tool of choice.

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

Specifying your own difftool, on the other hand, takes a little bit more work, see How do I view 'git diff' output with my preferred diff tool/ viewer?

另一方面,指定您自己的 difftool 需要更多的工作,请参阅如何使用我喜欢的 diff 工具/查看器查看“git diff”输出?

回答by Omer Dagan

An additional way to do that (from the command line):

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

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

The first two lines will set the difftool and mergetool to tkdiff- change that according to your preferences. The third line disables the annoying prompt so whenever you hit git difftoolit will automatically launch the difftool.

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

回答by tgoza

Others have done a 99% answer on this, but there is one step left out. (My answer will be coming from OS X so you will have to change file paths accordingly.)

其他人对此做出了 99% 的回答,但还遗漏了一步。(我的答案将来自 OS X,因此您必须相应地更改文件路径。)

You make these changes to your ~/.gitconfig:

您对您的 进行了这些更改~/.gitconfig

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

This will fix the diff tool. You can also fix this without editing the ~/.gitconfigdirectly by entering these commands from the terminal:

这将修复差异工具。您还~/.gitconfig可以通过从终端输入以下命令来解决此问题,而无需直接编辑:

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

The 1% that everyone else failed to mention is when using this you can't just run git diff myfile.txt; you need to run git difftool myfile.txt.

其他人没有提到的 1% 是在使用它时你不能只是运行git diff myfile.txt;你需要运行git difftool myfile.txt

回答by Dan Ray

Here's the part of my ~/.gitconfig where I configure diff and merge tools. I like diffmerge by SourceGear. (I like it very very much, as a matter of fact).

这是我的 ~/.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\"

So, you see, you're defining a tool named "diffmerge" in the [difftool "diffmerge"]line. Then I'm setting the tool "diffmerge" as the default in the [diff] tool =section.

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

I obviously have the "diffmerge" command in my path, here. Otherwise I'd need to give a full path to the executable.

我的路径中显然有“diffmerge”命令,这里。否则我需要提供可执行文件的完整路径。

回答by RBT

Reproducing my answer from thisthread which was more specific to setting beyond compare as diff tool for Git. All the details that I've shared are equally useful for any diff tool in general so sharing it here:

这个线程中复制我的答案,它更具体地用于设置作为 Git 的差异工具的比较。我分享的所有细节通常对任何差异工具同样有用,因此在此处分享:

The first command that we run is as below:

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

git config --global diff.tool bc3

The above command creates below entry in .gitconfigfound in %userprofile%directory:

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

[diff]
    tool = bc3

Then you run below command (Running this command is redundant in this particular case and is required in some specialized cases only. You will know it in a short while):

然后你运行下面的命令(在这种特殊情况下运行这个命令是多余的,只有在某些特殊情况下才需要。你很快就会知道它):

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

Above command creates below entry in .gitconfigfile:

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

[difftool "bc3"]
    path = c:/program files/Beyond Compare 3/bcomp.exe

The thing to know here is the key bc3. This is a well known key to git corresponding to a particular version of well known comparison tools available in market (bc3corresponds to 3rd version of Beyond Compare tool). If you want to see all pre-defined keys just run git difftool --tool-helpcommand on git bash. It returns below list:

这里要知道的事情是关键bc3。这是一个众所周知的 git 密钥,对应于市场上可用的知名比较工具的特定版本(bc3对应于 Beyond Compare 工具的第 3 版)。如果您想查看所有预定义的键,只需git difftool --tool-help在 git bash 上运行命令。它返回以下列表:

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

You can use any of the above keys or define a custom key of your own. If you want to setup a new tool altogether(or a newly released version of well-known tool) which doesn't map to any of the keys listed above then you are free to map it to any of keys listed above or to a newcustom key of your own.

您可以使用上述任何键或定义您自己的自定义键。如果您想设置一个未映射到上面列出的任何键的新工具(或新发布的知名工具版本),那么您可以自由地将它映射到上面列出的任何键或一个新的您自己的自定义密钥。

What if you have to setup a comparison tool which is

如果您必须设置一个比较工具怎么办

  • Absolutely new in market
  • 绝对新鲜的市场

OR

或者

  • A new version of an existing well known tool has got released which is not mapped to any pre-defined keys in git?
  • 现有知名工具的新版本已发布,但未映射到 git 中的任何预定义键

Like in my case, I had installed beyond compare 4. beyond compare is a well-known tool to git but its version 4 release is not mapped to any of the existing keys by default. So you can follow any of the below approaches:

就像在我的情况下,我安装了 Beyond compare 4。 Beyond compare 是一个众所周知的 git 工具,但它的第 4 版默认没有映射到任何现有的键。因此,您可以遵循以下任何一种方法:

  1. I can map beyond compare 4 tool to already existing key bc3which corresponds to beyond compare 3 version. I didn't have beyond compare version 3 on my computer so I didn't care. If I wanted I could have mapped it to any of the pre-defined keys in the above list also e.g. examdiff.

    If you map well known version of tools to appropriate already existing/well- known key then you would not need to run the second command as their install path is already known to git.

    For e.g. if I had installed beyond compare version 3 on my box then having below configuration in my .gitconfigfile would have been sufficient to get going:

    [diff]
    tool = bc3
    

    But if you want to change the default associated tool then you end up mentioning the pathattribute separately so that git gets to know the path from where you new tool's exe has to be launched. Here is the entry which foxes git to launch beyond compare 4 instead. Note the exe's path:

    [difftool "bc3"]
    path = c:/program files/Beyond Compare 4/bcomp.exe
    
  2. Most cleanest approachis to define a new key altogether for the new comparison tool or a new version of an well known tool. Like in my case I defined a new key bc4so that it is easy to remember. In such a case you have to run two commands in all but your second command will not be setting path of your new tool's executable. Instead you have to set cmdattribute for your 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 .gitconfigfile:

    [diff]
    tool = bc4
    [difftool "bc4"]
    cmd = \"C:\Program Files\Beyond Compare 4\bcomp.exe\" -s \"$LOCAL\" -d \"$REMOTE\"
    
  1. 我可以将 Beyond compare 4 工具映射到bc3与 Beyond compare 3 版本相对应的现有密钥。我没有在我的电脑上比较版本 3,所以我不在乎。如果我愿意,我可以将它映射到上面列表中的任何预定义键,例如examdiff.

    如果您将众所周知的工具版本映射到适当的现有/众所周知的密钥,那么您将不需要运行第二个命令,因为 git 已经知道它们的安装路径

    例如,如果我在我的盒子上安装了超出比较版本 3 的版本,那么在我的.gitconfig文件中具有以下配置就足够了:

    [diff]
    tool = bc3
    

    但是,如果您想更改默认的关联工具,那么您最终会path单独提及该属性,以便 git 了解必须启动新工具的 exe 的路径。这是使 git 启动而不是比较 4 的条目。注意exe的路径:

    [difftool "bc3"]
    path = c:/program files/Beyond Compare 4/bcomp.exe
    
  2. 最简洁的方法是为新的比较工具或知名工具的新版本定义一个新的密钥。就像在我的情况下,我定义了一个新密钥,bc4以便于记住。在这种情况下,您必须同时运行两个命令,但第二个命令不会设置新工具可执行文件的路径。相反,您必须cmd为新工具设置属性,如下所示:

    git config --global diff.tool bc4
    
    git config --global difftool.bc4.cmd "\"C:\Program Files\Beyond Compare 4\bcomp.exe\" -s \"$LOCAL\" -d \"$REMOTE\""
    

    运行上面的命令会在您的.gitconfig文件中创建以下条目:

    [diff]
    tool = bc4
    [difftool "bc4"]
    cmd = \"C:\Program Files\Beyond Compare 4\bcomp.exe\" -s \"$LOCAL\" -d \"$REMOTE\"
    

I would strongly recommend you to follow approach # 2 to avoid any confusion for yourself in future.

我强烈建议您遵循方法#2,以避免将来对自己造成任何混淆。

回答by moodboom

Adding one of the blocks below works for me to use KDiff3for my Windows and Linux development environments. It makes for a nice consistent cross-platform diff and merge tool.

添加以下块之一对我有用,以便在我的 Windows 和 Linux 开发环境中使用KDiff3。它是一个很好的一致的跨平台差异和合并工具。

Linux

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

Windows

视窗

[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

回答by Alexander Katz

If you want to have an option to use multiple diff tools add an alias to .gitconfig

如果您想选择使用多个差异工具,请向 .gitconfig 添加别名

[alias]
    kdiff = difftool --tool kdiff3

回答by sudoz

Refer to Microsoft vscode-tips-and-tricks. Just run these commands in your terminal:

请参阅 Microsoft vscode-tips-and-tricks。只需在终端中运行这些命令:

git config --global merge.tool code

git config --global merge.tool code

But firstly you need add codecommand to your PATH. enter image description here

但首先你需要code在你的 PATH 中添加命令。 在此处输入图片说明

回答by Goyal Vicky

In Windows we need to run $git difftool --tool-helpcommand to see the various options like:

在 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.

and we can add any of them(for example winmerge) like

我们可以添加其中任何一个(例如 winmerge),例如

$  git difftool --tool=winmerge

For configuring notepad++ to see files before committing:

用于配置 notepad++ 以在提交前查看文件:

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

and using $ git commitwill open the commit information in notepad++

并使用$ git commit将在记事本++中打开提交信息