如何让 diff 像 git-diff 一样工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4857310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:59:43  来源:igfitidea点击:

How to get diff working like git-diff?

gitdiffgit-diff

提问by Mzzzzzz

I like the output formatting of git diff. The color and the +/-representation of changes between lines is easier to read than GNU diff.

我喜欢git diff. 行间变化的颜色和+/-表示比 GNU diff 更容易阅读。

I can run git diffusing --no-indexflag outside of a git repo and it works fine. However, it appears to be missing the --excludeoption for excluding files or subdirectories from a recursive diff.

我可以在git repo 之外git diff使用--no-indexflag运行,它工作正常。但是,它似乎缺少--exclude从递归diff.

Is there a way to get the best of both worlds? (color options and +/-format of git diffand --excludeoption of GNU diff).

有没有办法两全其美?(GNU diff 的颜色选项和+/-格式git diff--exclude选项)。

I've experimented with colordiff, but I still prefer the output format of git diff

我已经尝试过colordiff,但我仍然更喜欢的输出格式git diff

回答by jonescb

I don't know how to do color but this will do the +/-rather than <and >.

我不知道如何做颜色,但这会做+/-而不是<>

diff -u file1 file2

回答by eacousineau

You can also use git diff --no-index -- A B(via manpage).

您也可以使用git diff --no-index -- A B(通过manpage)。

回答by Steve

  1. Install colordiff.

  2. Update your ~/.colordiffrc (copying /etc/colordiffrc first, if necessary):

    # be more git-like:
    plain=off
    newtext=darkgreen
    oldtext=darkred
    diffstuff=darkcyan
    
  3. Use colordiff -u file1 file2for two files or colordiff -ruN path1 path2for recursively comparing paths.

  1. 安装colordiff

  2. 更新您的 ~/.colordiffrc(如有必要,请先复制 /etc/colordiffrc):

    # be more git-like:
    plain=off
    newtext=darkgreen
    oldtext=darkred
    diffstuff=darkcyan
    
  3. 使用colordiff -u file1 file2了两个文件或colordiff -ruN path1 path2用于递归比较路径。

It's not exactly the same, but it's very close.

它不完全相同,但非常接近。

回答by Nate

This is what I suggest and it's pretty close

这就是我的建议,而且非常接近

diff -u FILE1 FILE2 | colordiff | less -R
  • colordiff: You'll have to install this
  • -R: this tells Less to show colors instead of the raw codes.
  • colordiff: 你必须安装这个
    • brew install colordiff在我的 Mac 上。
    • port install colordiff在某些 Mac 上。
    • sudo apt-get install colordiff在 Debian 或 Ubuntu 上
    • 对于其他平台,从主页GitHub下载源并按照安装说明进行操作
  • -R:这告诉 Less 显示颜色而不是原始代码。

I ultimately used -wbecause I didn't want to see whitespace diffs.

我最终使用了-w因为我不想看到空白差异。

diff -w -u FILE1 FILE2 | colordiff | less -R

Edit: As suggested by @Ciprian Tomoiaga in the comment, you can make this a function and put it in your ~/.bashrcfile too.

编辑:正如@Ciprian Tomoiaga 在评论中所建议的,您可以将其设为函数并将其放入您的~/.bashrc文件中。

function gdiff () { diff -u $@ | colordiff | less -R; }

回答by codehearts

GNU diffhas a --coloroption since version 3.4 in late 2016 according to this answeron the Unix SE. That alongside -ushould be enough to mimic the output of git diff:

根据Unix SE 上的这个答案,GNU自 2016 年末的 3.4 版起diff有一个--color选项。旁边应该足以模仿的输出:-ugit diff

diff -u --color=always file1 file2 | less -r

diff -u --color=always file1 file2 | less -r

--colormust be alwayswhen used in a pipe, autowill turn off color in pipes.

--color必须always在管道中使用时,auto将关闭管道中的颜色。

I've only tried this with Git Bash on Windows, where less -Rwould only color the first line of a hunk. less -rfixed it for me in that case.

我只在 Windows 上用 Git Bash 尝试过这个,那里less -R只会给大块的第一行上色。less -r在那种情况下为我修好了。

回答by Dejay Clayton

Using only bash, diff, tput, and less, we can closely approximate the output of git diff. There will be some notable differences, though, due to the short-sightedness of the diffprogrammers.

仅使用bashdifftput、 和less,我们可以非常接近 的输出git diff。但是,由于diff程序员的短视,会有一些显着的差异。

Put the following Bash function definition in some file that gets sourced automatically by your user account, and you'll be able to access the function from the command line:

将以下 Bash 函数定义放在由您的用户帐户自动获取的某个文件中,您将能够从命令行访问该函数:

function gdiff()
{
    local REG=`tput op`
    local GRP=`tput setaf 6`
    local ADD=`tput setaf 2`
    local REM=`tput setaf 1`

    local NL=$'\n'
    local GRP_LABEL="${GRP}@@ %df,%dn +%dF,%dN @@${REG}"

    local UNCH_GRP_FMT=''

    [[ "" == '@full' ]] && {

        UNCH_GRP_FMT="${GRP_LABEL}${NL}%="
        shift
    }

    diff \
        --new-line-format="${ADD}+%L${REG}" \
        --old-line-format="${REM}-%L${REG}" \
        --unchanged-line-format=" %L${REG}" \
        --new-group-format="${GRP_LABEL}${NL}%>" \
        --old-group-format="${GRP_LABEL}${NL}%<" \
        --changed-group-format="${GRP_LABEL}${NL}%<%>" \
        --unchanged-group-format="${UNCH_GRP_FMT}" \
            "${@}" | less -FXR
}

This function works as follows:

该函数的工作原理如下:

  1. Ultimately, diffgets invoked with various formatting options to specify how changes within the files will be displayed.
  2. tputis used to insert ANSI color codes into those formatting options. Note that when using non-ANSI terminals, you may have to replace tput setafwith tput setf.
  3. The output of diffis piped into less. -Rallows ANSI colors to be preserved. -Xprevents lessfrom clearing the screen upon exiting. -Fprevents lessfrom operating as a pager if the output fits within one screen.
  4. If the first parameter is @full, the function will display all unchanged lines in addition to added and removed lines.
  1. 最终,diff使用各种格式选项调用以指定如何显示文件中的更改。
  2. tput用于将 ANSI 颜色代码插入到这些格式选项中。请注意,在使用非 ANSI 终端时,您可能需要替换tput setaftput setf.
  3. 的输出通过diff管道传输到less. -R允许保留 ANSI 颜色。 -X防止less退出时清屏。 如果输出适合一个屏幕,则-F阻止其less作为寻呼机运行。
  4. 如果第一个参数是@full,该函数将显示除添加和删除的行之外的所有未更改的行。

Note the following differences between this approach and git diff:

请注意此方法与 之间的以下差异git diff

  1. git diffreports three lines of context surrounding each change. Unfortunately, diffseems to complain and exit if you want to specify the number of context lines while also simultaneously specifying formatting options. (At least it does in Mac OS X Yosemite). Thanks diffprogrammers. Therefore, you can either request no lines of context surrounding each change, which is the default behavior, or you can request that all unchanged lines within the file are also reported, by specifying @fullas the first parameter.
  2. Because the lines of context are different from git diff, the line numbers reported by this function will also vary from those reported by git diff.
  3. You may see the presence of single-line changes reported, which is the correct behavior, but annoying when your changed file contains the insertion of single empty lines. I think git diffdeals with this better, via its lines of context. You could try passing different options to diffto better deal with whitespace, if you prefer.
  1. git diff报告围绕每个更改的三行上下文。不幸的是,diff如果您想指定上下文行的数量同时还指定格式选项,似乎会抱怨并退出。(至少在 Mac OS X Yosemite 中是这样)。谢谢diff程序员。因此,您可以请求没有围绕每个更改的上下文行,这是默认行为,或者您可以通过指定@full为第一个参数来请求也报告文件中所有未更改的行。
  2. 由于上下文的行与 不同git diff,因此此函数报告的行号也将与 报告的行号不同git diff
  3. 您可能会看到报告的单行更改的存在,这是正确的行为,但是当您更改的文件包含单个空行的插入时会很烦人。我认为git diff通过它的上下文可以更好地处理这个问题。diff如果您愿意,您可以尝试传递不同的选项以更好地处理空格。

回答by himanshuxd

Place this in your .bashrcor .zshrc:

把它放在你的.bashrc.zshrc

diff() { git diff --no-index "$1" "$2" | colordiff; }

diff() { git diff --no-index "$1" "$2" | colordiff; }

requirments : gitand colordiffshould be installed before-hand.

要求:git并且colordiff应该事先安装。

usage : diff file1 file2

用法 : diff file1 file2

example : for $diff .tmux.conf .zshrc.pre-oh-my-zsh

示例:对于 $diff .tmux.conf .zshrc.pre-oh-my-zsh

diff function example

差异函数示例

回答by Davoud Taghawi-Nejad

You are looking for colordiff:

您正在寻找colordiff

sudo apt-get install colordiff

回答by Rakmo

Use colordiff:

使用颜色差异

Installation:

安装:

sudo apt-get install colordiff

Usage:

用法:

colordiff -u file_one file_two

Gives exactly same difference as shown by git diff.

给出完全相同的差异,如 所示git diff

回答by Syrtis Major

If you don't have colordiffor git diff, you can get color by vim.

如果您没有colordiffgit diff,则可以通过 获取颜色vim

cdiff() { diff -u $@ | vim -R -; }

or simply

或者干脆

cdiff() { diff -u $@ | view -; }