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


当前回答

其他几个人已经提到了用于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源代码进行高级用户定制。

其他回答

export GIT_EXTERNAL_DIFF='meld $2 $5; echo >/dev/null'

然后简单的:

git 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中测试。

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

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

我用色差。

在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会得到更好的结果)