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


当前回答

你可以使用sdiff做一个并排的diff,如下所示:

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

其中HEAD^是一个例子,你应该用任何你想要反对的东西来替换它。

我在这里找到了这个解决方案,还有一些其他的建议。然而,这一个答案是OP的问题简洁而清楚。

有关这些论点的解释,请参阅man git-difftool。


把注释记在板上,你可以通过编写以下可执行脚本创建一个方便的git sdiff命令:

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

保存为/usr/bin/git-sdiff并chmod +x它。然后你就可以这样做了:

$ git sdiff HEAD^

额外的小费

正如评论中所建议的,你可以使用icdiff来做sdiff对彩色输出所做的事情:

$ more /usr/bin/git-sdiff
#!/bin/sh
git difftool -y -x "icdiff --cols $(tput cols)" "${@}" | less --raw-control-chars

其他回答

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

将其命名为“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

你可以使用sdiff做一个并排的diff,如下所示:

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

其中HEAD^是一个例子,你应该用任何你想要反对的东西来替换它。

我在这里找到了这个解决方案,还有一些其他的建议。然而,这一个答案是OP的问题简洁而清楚。

有关这些论点的解释,请参阅man git-difftool。


把注释记在板上,你可以通过编写以下可执行脚本创建一个方便的git sdiff命令:

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

保存为/usr/bin/git-sdiff并chmod +x它。然后你就可以这样做了:

$ git sdiff HEAD^

额外的小费

正如评论中所建议的,你可以使用icdiff来做sdiff对彩色输出所做的事情:

$ more /usr/bin/git-sdiff
#!/bin/sh
git difftool -y -x "icdiff --cols $(tput cols)" "${@}" | less --raw-control-chars

当我在寻找一种使用git内置方式来定位差异的快速方法时,这个问题出现了。我的解决方案标准:

快速启动,需要内置选项 可以处理多种格式,xml,不同的编程语言 快速识别大文本文件中的小代码更改

我找到这个答案是为了给git上色。

为了获得并排diff而不是行diff,我调整了mb14在这个问题上的优秀答案,使用以下参数:

$ git diff --word-diff-regex="[A-Za-z0-9. ]|[^[:space:]]"

如果你不喜欢额外的[-或{+选项——word-diff=color可以使用。

$ git diff --word-diff-regex="[A-Za-z0-9. ]|[^[:space:]]" --word-diff=color

这有助于与json和xml文本以及java代码进行适当的比较。

总之,——word-diff-regex选项与颜色设置一起具有非常有用的可见性,与标准行差异相比,当浏览带有小行更改的大文件时,可以获得彩色的并排源代码体验。

这里有一个方法。如果管道通过less, xterm宽度设置为80,这不是很热。但是如果继续执行命令,例如COLS=210,则可以使用扩展的xterm。

gitdiff()
{
    local width=${COLS:-$(tput cols)}
    GIT_EXTERNAL_DIFF="diff -yW$width \$2 \$5; echo >/dev/null" git diff "$@"
}

这可能是一个有点有限的解决方案,但在没有外部工具的情况下使用系统的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