当我输入git diff时,我希望看到一个并排的diff,就像用diff -y一样,或者像在kdiff3这样的交互式diff工具中显示diff。如何做到这一点呢?


当前回答

我用色差。

在Mac OS X上,使用

$ sudo port install colordiff

在Linux上可能会安装colordiff或类似的东西,这取决于你的发行版。

然后:

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

或者创建别名

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

然后你就可以使用它了

$ git diffy HEAD^ HEAD

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

其他回答

我个人非常喜欢icdiff !

如果你在Mac OS X上使用HomeBrew,只需安装icdiff即可。

为了获得正确的文件标签,加上其他很酷的功能,我在~/.gitconfig中有:

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

我用git difftool

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

$ pip install webdiff
$ git webdiff

与传统的GUI差异工具(如tkdiff)相比,这提供了许多优势,因为它可以为您提供语法高亮显示和显示图像差异。

点击这里阅读更多信息。

在这个帖子里有很多很好的答案。对于这个问题,我的解决方案是编写一个脚本。

将其命名为“git-scriptname”(并使其可执行并将其放在您的PATH中,就像任何脚本一样),您可以像正常的git命令一样通过运行调用它

$ git scriptname

实际的功能在最后一行。来源如下:

#!/usr/bin/env zsh
#
#   Show a side-by-side diff of a particular file how it currently exists between:
#       * the file system
#       * in HEAD (latest committed changes)

function usage() {
    cat <<-HERE
    USAGE

    $(basename $1) <file>

    Show a side-by-side diff of a particular file between the current versions:

        * on the file system (latest edited changes)
        * in HEAD (latest committed changes)

HERE
}

if [[ $# = 0 ]]; then
    usage $0
    exit
fi

file=$1
diff -y =(git show HEAD:$file) $file | pygmentize -g | less -R

这可能是一个有点有限的解决方案,但在没有外部工具的情况下使用系统的diff命令完成工作:

diff -y  <(git show from-rev:the/file/path) <(git show to-rev:the/file/path)

仅过滤更改行使用——suppress-common-lines(如果您的diff支持该选项)。 在这种情况下,没有颜色,只有通常的不同标记 可以调整列宽度-width=term-width;在Bash中可以获取宽度为$COLUMNS或tput cols。

为了更方便,这也可以被包装到一个helper git-script中,例如,这样使用:

git diffy the/file/path --from rev1 --to rev2

对于unix,只组合git和内置的diff:

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

当然,您可以用任何其他git引用替换HEAD,并且您可能希望在diff命令中添加-W 170之类的东西。

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

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

其中REF1和REF2是git引用-标签,分支或哈希。