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


当前回答

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

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

其他回答

伊迪夫

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

而不是做git diff,做:

ydiff -s -w0

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

安装:

python3 -m pip install --user ydiff

-or-

brew install ydiff

对于git日志,您可以使用:

ydiff -ls -w0

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

在Git 2.18.0, ydiff 1.1中测试。

你可以使用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

我个人非常喜欢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

我最近实现了一个这样做的工具:https://github.com/banga/git-split-diffs

下面是如何使用它:

npm install -g git-split-diffs

git config --global core.pager "git-split-diffs --color | less -RFX"

这是它在终端中的外观(使用默认主题):

如您所见,它还支持语法高亮显示和行中更改的单词高亮显示

打开Intellij IDEA,在“版本控制”工具窗口中选择单个或多个提交,浏览更改的文件,然后双击它们,并排检查每个文件的更改。

使用捆绑的命令行启动器,您可以在任何地方使用简单的IDEA some/path启动IDEA