当我输入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

其他回答

尝试 git difftool

使用git difftool而不是git diff,你将永远不会回到过去。

UPDATE添加一个示例用法:

这是另一个关于git difftool的stackoverflow的链接:我如何用我喜欢的diff工具/查看器查看'git diff'输出?

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

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

其他支持的外部diff工具通过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
export GIT_EXTERNAL_DIFF='meld $2 $5; echo >/dev/null'

然后简单的:

git 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

其他几个人已经提到了用于git的cdiff,但没有人给出它的完整实现。

设置cdiff:

git clone https://github.com/ymattw/cdiff.git
cd cdiff
ln -s `pwd`/cdiff ~/bin/cdiff
hash -r # refresh your PATH executable in bash (or 'rehash' if you use tcsh)
        # or just create a new terminal

编辑~ /。Gitconfig插入这些行:

[pager]
        diff = false
        show = false

[diff]
        tool = cdiff
        external = "cdiff -s $2 $5 #"

[difftool "cdiff"]
        cmd = cdiff -s \"$LOCAL\" \"$REMOTE\"

[alias]
        showw = show --ext-dif

关闭寻呼机是cdiff与Diff一起工作所必需的,它本质上是一个寻呼机,所以这很好。不管这些设置,Difftool都能正常工作。

需要show别名,因为git show仅通过参数支持外部差异工具。

diff external命令后面的“#”很重要。Git的diff命令向diff命令追加了一个$@(所有可用的diff变量),但我们只想要这两个文件名。因此,我们用$2和$5显式地调用这两个,然后将$@隐藏在注释后面,否则会使sdiff混淆。导致如下错误:

fatal: <FILENAME>: no such path in the working tree
Use 'git <command> -- <path>...' to specify paths that do not exist locally.

Git命令现在产生并排差异:

git diff <SHA1> <SHA2> 
git difftool <SHA1> <SHA2>
git showw <SHA>

Cdiff usage:

'SPACEBAR' - Advances the page of the current file.
'Q'        - Quits current file, thus advancing you to the next file.

你现在通过git diff和difftool得到了并行的diff。如果需要,还可以使用cdiff python源代码进行高级用户定制。

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

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