如何让 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
How to get diff working like git-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 diff
using --no-index
flag outside of a git repo and it works fine. However, it appears to be missing the --exclude
option for excluding files or subdirectories from a recursive diff
.
我可以在git repo 之外git diff
使用--no-index
flag运行,它工作正常。但是,它似乎缺少--exclude
从递归diff
.
Is there a way to get the best of both worlds? (color options and +
/-
format of git diff
and --exclude
option 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
回答by Steve
Install colordiff.
Update your ~/.colordiffrc (copying /etc/colordiffrc first, if necessary):
# be more git-like: plain=off newtext=darkgreen oldtext=darkred diffstuff=darkcyan
Use
colordiff -u file1 file2
for two files orcolordiff -ruN path1 path2
for recursively comparing paths.
安装colordiff。
更新您的 ~/.colordiffrc(如有必要,请先复制 /etc/colordiffrc):
# be more git-like: plain=off newtext=darkgreen oldtext=darkred diffstuff=darkcyan
使用
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 thisbrew install colordiff
on my Mac.port install colordiff
on some Macs.sudo apt-get install colordiff
on Debian or Ubuntu- For other platforms, download the source from the main pageor GitHuband follow the installation instructions
-R
: this tells Less to show colors instead of the raw codes.
colordiff
: 你必须安装这个-R
:这告诉 Less 显示颜色而不是原始代码。
I ultimately used -w
because 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 ~/.bashrc
file too.
编辑:正如@Ciprian Tomoiaga 在评论中所建议的,您可以将其设为函数并将其放入您的~/.bashrc
文件中。
function gdiff () { diff -u $@ | colordiff | less -R; }
回答by codehearts
GNU diff
has a --color
option since version 3.4 in late 2016 according to this answeron the Unix SE. That alongside -u
should be enough to mimic the output of git diff
:
根据Unix SE 上的这个答案,GNU自 2016 年末的 3.4 版起diff
有一个--color
选项。旁边应该足以模仿的输出:-u
git diff
diff -u --color=always file1 file2 | less -r
diff -u --color=always file1 file2 | less -r
--color
must be always
when used in a pipe, auto
will turn off color in pipes.
--color
必须always
在管道中使用时,auto
将关闭管道中的颜色。
I've only tried this with Git Bash on Windows, where less -R
would only color the first line of a hunk. less -r
fixed 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 diff
programmers.
仅使用bash
、diff
、tput
、 和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:
该函数的工作原理如下:
- Ultimately,
diff
gets invoked with various formatting options to specify how changes within the files will be displayed. tput
is used to insert ANSI color codes into those formatting options. Note that when using non-ANSI terminals, you may have to replacetput setaf
withtput setf
.- The output of
diff
is piped intoless
.-R
allows ANSI colors to be preserved.-X
preventsless
from clearing the screen upon exiting.-F
preventsless
from operating as a pager if the output fits within one screen. - If the first parameter is
@full
, the function will display all unchanged lines in addition to added and removed lines.
- 最终,
diff
使用各种格式选项调用以指定如何显示文件中的更改。 tput
用于将 ANSI 颜色代码插入到这些格式选项中。请注意,在使用非 ANSI 终端时,您可能需要替换tput setaf
为tput setf
.- 的输出通过
diff
管道传输到less
.-R
允许保留 ANSI 颜色。-X
防止less
退出时清屏。 如果输出适合一个屏幕,则-F
阻止其less
作为寻呼机运行。 - 如果第一个参数是
@full
,该函数将显示除添加和删除的行之外的所有未更改的行。
Note the following differences between this approach and git diff
:
请注意此方法与 之间的以下差异git diff
:
git diff
reports three lines of context surrounding each change. Unfortunately,diff
seems 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). Thanksdiff
programmers. 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@full
as the first parameter.- Because the lines of context are different from
git diff
, the line numbers reported by this function will also vary from those reported bygit diff
. - 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 diff
deals with this better, via its lines of context. You could try passing different options todiff
to better deal with whitespace, if you prefer.
git diff
报告围绕每个更改的三行上下文。不幸的是,diff
如果您想指定上下文行的数量同时还指定格式选项,似乎会抱怨并退出。(至少在 Mac OS X Yosemite 中是这样)。谢谢diff
程序员。因此,您可以请求没有围绕每个更改的上下文行,这是默认行为,或者您可以通过指定@full
为第一个参数来请求也报告文件中所有未更改的行。- 由于上下文的行与 不同
git diff
,因此此函数报告的行号也将与 报告的行号不同git diff
。 - 您可能会看到报告的单行更改的存在,这是正确的行为,但是当您更改的文件包含单个空行的插入时会很烦人。我认为
git diff
通过它的上下文可以更好地处理这个问题。diff
如果您愿意,您可以尝试传递不同的选项以更好地处理空格。
回答by himanshuxd
Place this in your .bashrc
or .zshrc
:
把它放在你的.bashrc
或.zshrc
:
diff() { git diff --no-index "$1" "$2" | colordiff; }
diff() { git diff --no-index "$1" "$2" | colordiff; }
requirments : git
and colordiff
should 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
回答by Davoud Taghawi-Nejad
回答by Rakmo
回答by Syrtis Major
If you don't have colordiff
or git diff
, you can get color by vim
.
如果您没有colordiff
或git diff
,则可以通过 获取颜色vim
。
cdiff() { diff -u $@ | vim -R -; }
or simply
或者干脆
cdiff() { diff -u $@ | view -; }