当我执行“git diff”时如何获得并排差异?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7669963/
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 12:00:36  来源:igfitidea点击:

How can I get a side-by-side diff when I do "git diff"?

gitgit-diffcode-visualization

提问by

When I type "git diff", I'd like to see a side-by-side diff, like with "diff -y", or like to display the diff in an interactive diff tool like "kdiff3". How can this be done?

当我输入“git diff”时,我希望看到一个并排的差异,比如“diff -y”,或者喜欢在“kdiff3”这样的交互式差异工具中显示差异。如何才能做到这一点?

采纳答案by Tilo

Although Git has an internal implementation of diff, you can set up an external tool instead.

尽管 Git 有 diff 的内部实现,但您可以设置一个外部工具来代替。

There are two different ways to specify an external diff tool:

有两种不同的方法可以指定外部差异工具:

  1. setting the GIT_EXTERNAL_DIFFand the GIT_DIFF_OPTSenvironment variables.
  2. configuring the external diff tool via git config
  1. 设置GIT_EXTERNAL_DIFFGIT_DIFF_OPTS环境变量。
  2. 通过配置外部差异工具 git config

See also:

也可以看看:

When doing a git diff, Git checks both the settings of above environment variables and its .gitconfigfile.

在执行 a 时git diff,Git 检查上述环境变量的设置及其.gitconfig文件。

By default, Git passes the following seven arguments to the diff program:

默认情况下,Git 将以下七个参数传递给 diff 程序:

path  old-file  old-hex old-mode  new-file  new-hex new-mode

You typically only need the old-file and new-file parameters. Of course most diff tools only take two file names as an argument. This means that you need to write a small wrapper-script, which takes the arguments which Git provides to the script, and hands them on to the external git program of your choice.

您通常只需要旧文件和新文件参数。当然,大多数差异工具仅将两个文件名作为参数。这意味着您需要编写一个小的包装器脚本,它接受 Git 提供给脚本的参数,并将它们传递给您选择的外部 git 程序。

Let's say you put your wrapper-script under ~/scripts/my_diff.sh:

假设您将包装脚本放在以下位置~/scripts/my_diff.sh

#!/bin/bash
# un-comment one diff tool you'd like to use

# side-by-side diff with custom options:
# /usr/bin/sdiff -w200 -l "" "" 

# using kdiff3 as the side-by-side diff:
# /usr/bin/kdiff3 "" ""

# using Meld 
/usr/bin/meld "" ""

# using VIM
# /usr/bin/vim -d "" ""

you then need to make that script executable:

然后您需要使该脚本可执行:

chmod a+x ~/scripts/my_diff.sh

you then need to tell Git how and where to find your custom diff wrapper script. You have three choices how to do that: (I prefer editing the .gitconfig file)

然后,您需要告诉 Git 如何以及在哪里找到您的自定义差异包装器脚本。你有三个选择如何做到这一点:(我更喜欢编辑 .gitconfig 文件)

  1. Using GIT_EXTERNAL_DIFF, GIT_DIFF_OPTS

    e.g. in your .bashrc or .bash_profile file you can set:

    GIT_EXTERNAL_DIFF=$HOME/scripts/my_diff.sh
    export GIT_EXTERNAL_DIFF
    
  2. Using git config

    use "git config" to define where your wrapper script can be found:

    git config --global diff.external ~/scripts/my_diff.sh
    
  3. Editing your ~/.gitconfigfile

    you can edit your ~/.gitconfigfile to add these lines:

    [diff]
      external = ~/scripts/my_diff.sh
    
  1. 使用GIT_EXTERNAL_DIFF,GIT_DIFF_OPTS

    例如,在您的 .bashrc 或 .bash_profile 文件中,您可以设置:

    GIT_EXTERNAL_DIFF=$HOME/scripts/my_diff.sh
    export GIT_EXTERNAL_DIFF
    
  2. 使用 git config

    使用“git config”来定义可以在何处找到您的包装器脚本:

    git config --global diff.external ~/scripts/my_diff.sh
    
  3. 编辑您的~/.gitconfig文件

    您可以编辑~/.gitconfig文件以添加以下行:

    [diff]
      external = ~/scripts/my_diff.sh
    

Note:

笔记:

Similarly to installing your custom diff tool, you can also install a custom merge-tool, which could be a visual merging tool to better help visualizing the merge. (see the progit.org page)

与安装自定义差异工具类似,您还可以安装自定义合并工具,它可以是一个可视化合并工具,以更好地帮助可视化合并。(参见 progit.org 页面)

See: http://fredpalma.com/518/visual-diff-and-merge-tool/and https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

请参阅:http: //fredpalma.com/518/visual-diff-and-merge-tool/https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

回答by Matt Ball

Try git difftool

试试 git difftool

Use git difftoolinstead of git diff. You'll never go back.

使用git difftool代替git diff。你永远不会回去。

UPDATE to add an example usage:

更新以添加示例用法:

Here is a link to another stackoverflow that talks about git difftool: How do I view 'git diff' output with my preferred diff tool/ viewer?

这是另一个讨论的 stackoverflow 的链接git difftoolHow do I view 'git diff' output with my preferred diff tool/ viewer?

For newer versions of git, the difftoolcommand supports many external diff tools out-of-the-box. For example vimdiffis auto supported and can be opened from the command line by:

对于较新版本的git,该difftool命令支持许多开箱即用的外部差异工具。例如vimdiff支持自动,可以通过以下方式从命令行打开:

cd /path/to/git/repo
git difftool --tool=vimdiff

Other supported external diff tools are listed via git difftool --tool-helphere is an example output:

通过git difftool --tool-help这里列出了其他支持的外部差异工具是一个示例输出:

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

The following tools are valid, but not currently available:
        bc3
        codecompare
        deltawalker
        diffuse
        ecmerge
        emerge
        gvimdiff
        gvimdiff2
        kdiff3
        meld
        opendiff
        tkdiff
        xxdiff

回答by mb14

You can also try git diff --word-diff. It's not exactly side-by-side, but somehow better, so you might prefer it to your actual side-by-side need.

你也可以试试git diff --word-diff。它不是完全并排的,但不知何故更好,因此您可能更喜欢它而不是您实际的并排需要。

回答by ymattw

ydiff

ydiff

Formerly called cdiff, this tool can display side by side, incremental, and colorfuldiff.

以前称为cdiff,此工具可以显示并排增量彩色差异。

Instead of doing git diff, do:

而不是做git diff,做:

ydiff -s -w0

This will launch ydiffin side-by-side display mode for each of the files with differences.

这将ydiff在并排显示模式下为每个有差异的文件启动。

Install with:

安装:

python3 -m pip install --user ydiff

For git log, you can use:

对于git log,您可以使用:

ydiff -ls -w0

-w0auto-detects your terminal width. See the ydiffGitHub repository pagefor detail and demo.

-w0自动检测您的终端宽度。有关详细信息和演示,请参阅ydiffGitHub 存储库页面

Tested in Git 2.18.0, ydiff 1.1.

在 Git 2.18.0、ydiff 1.1 中测试。

回答by starfry

You can do a side-by-side diffusing sdiffas follows:

您可以并排diff使用sdiff,如下所示:

$ git difftool -y -x sdiff  HEAD^ | less

where HEAD^is an example that you should replace with whatever you want to diff against.

哪里HEAD^是一个例子,你应该用你想要比较的任何东西替换它。

I found this solution herewhere there are a couple of other suggestions also. However, this one answer's the OP's question succinctly and clearly.

我在这里找到了这个解决方案其中还有一些其他建议。但是,这个答案简洁明了地是 OP 的问题。

See the man git-difftoolfor an explanation of the arguments.

有关参数的解释,请参阅man git-difftool



Taking the comments on board, you can create a handy git sdiffcommand by writing the following executable script:

接受评论后,您可以git sdiff通过编写以下可执行脚本来创建一个方便的命令:

#!/bin/sh
git difftool -y -x "sdiff -w $(tput cols)" "${@}" | less

Save it as /usr/bin/git-sdiffand chmod -xit. Then you'll be able to do this:

另存为/usr/bin/git-sdiffchmod -x它。然后你将能够做到这一点:

$ git sdiff HEAD^

回答by krzych

export GIT_EXTERNAL_DIFF='meld  ; echo >/dev/null'

then simply:

然后简单地:

git diff

回答by danvk

If you'd like to see side-by-side diffs in a browser without involving GitHub, you might enjoy git webdiff, a drop-in replacement for git diff:

如果您希望在不涉及 GitHub 的情况下在浏览器中查看并排差异,您可能会喜欢git webdiff,它是 的替代品git diff

$ pip install webdiff
$ git webdiff

This offers a number of advantages over traditional GUI difftools like tkdiffin that it can give you syntax highlighting and show image diffs.

与传统的 GUI difftools 相比,这提供了许多优点,例如tkdiff它可以为您提供语法突出显示和显示图像差异。

Read more about it here.

在此处阅读更多相关信息。

回答by Luigi R. Viggiano

I use colordiff.

我使用colordiff

On Mac OS X, install it with

在 Mac OS X 上,安装它

$ sudo port install colordiff

On Linux is possibly apt get install colordiffor something like that, depending on your distro.

在 Linux 上可能是apt get install colordiff或类似的,这取决于您的发行版。

Then:

然后:

$ git difftool --extcmd="colordiff -ydw" HEAD^ HEAD

Or create an alias

或者创建一个别名

$ git alias diffy "difftool --extcmd=\"colordiff -ydw\""

Then you can use it

然后你可以使用它

$ git diffy HEAD^ HEAD

I called it "diffy" because diff -yis the side-by-side diff in unix. Colordiff also adds colors, that are nicer. In the option -ydw, the yis for the side-by-side, the wis to ignore whitespaces, and the dis to produce the minimal diff (usually you get a better result as diff)

我称它为“diffy”,因为它diff -y是 unix 中的并排差异。Colordiff 还添加了更好的颜色。在选项中-ydwy是并排的,w是忽略空格,d是产生最小的差异(通常你会得到更好的结果作为差异)

回答by Walker Hale IV

For unix, combining just gitand the built-in diff:

对于unix,结合 justgit和 built-in diff

git show HEAD:path/to/file | diff -y - path/to/file

Of course, you can replace HEAD with any other git reference, and you probably want to add something like -W 170to the diff command.

当然,您可以将 HEAD 替换为任何其他 git 引用,并且您可能希望向-W 170diff 命令添加类似的内容。

This assumes that you are just comparing your directory contents with a past commit. Comparing between two commits is more complex. If your shell is bashyou can use "process substitution":

这假设您只是将目录内容与过去的提交进行比较。比较两次提交比较复杂。如果你的 shell 是bash你可以使用“进程替换”:

diff -y -W 170 <(git show REF1:path/to/file) <(git show REF2:path/to/file)

where REF1and REF2are git references – tags, branches or hashes.

其中REF1REF2是 git 引用——标签、分支或哈希。

回答by Jose Alban

I personally really like icdiff!

我个人非常喜欢icdiff

If you're on Mac OS Xwith HomeBrew, just do brew install icdiff.

如果你在Mac OS XHomeBrew,只是做brew install icdiff

To get the file labels correctly, plus other cool features, I have in my ~/.gitconfig:

为了正确获取文件标签以及其他很酷的功能,我在我的~/.gitconfig

[pager]
    difftool = true
[diff]
    tool = icdiff
[difftool "icdiff"]
    cmd = icdiff --head=5000 --highlight --line-numbers -L \"$BASE\" -L \"$REMOTE\" \"$LOCAL\" \"$REMOTE\"

And I use it like: git difftool

我像这样使用它: git difftool