如何让 diff 像 git-diff 一样工作?
我喜欢 git diff 的输出格式。行间变化的颜色和 +
/-
表示比 GNU diff 更容易阅读。
我可以在 git 存储库之外使用 --no-index
标志运行 git diff
并且工作正常。但是,它似乎缺少用于从递归 diff
中排除文件或子目录的 --exclude
选项。
有没有一种方法可以两全其美? (git diff
的颜色选项和 +
/-
格式以及 GNU diff 的 --exclude
选项)。
我已经尝试过 colordiff
,但我仍然更喜欢 git diff
I like the output formatting of git diff
. The color and the +
/-
representation of changes between lines is easier to read than 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
.
Is there a way to get the best of both worlds? (color options and +
/-
format of git diff
and --exclude
option of GNU diff).
I've experimented with colordiff
, but I still prefer the output format of git diff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
这将执行
+/-
而不是<
和>
。从 GNU diffutils 3.4 开始,添加了标志
--color
。将两者结合起来会产生以下效果:标志
--color
也接受一个参数,有效选项为never
、always
或auto< /代码>。当您想要更明确地说明需要做什么时很有用。
This will do the
+/-
rather than<
and>
.Since GNU diffutils 3.4 the flag
--color
has been added. Combining both makes the following:The flag
--color
also takes an argument, valid options arenever
,always
, orauto
. Useful when you want to be more explicit on what needs to be done.您还可以使用 git diff --no-index -- A B (通过 手册页)。
You can also use
git diff --no-index -- A B
(via manpage).安装colordiff。
更新您的 ~/.colordiffrc (如有必要,首先复制 /etc/colordiffrc):
对两个文件或
colordiff 使用
用于递归比较路径。colordiff -u file1 file2
-ruN path1 path2虽然不完全一样,但是非常接近。
Install colordiff.
Update your ~/.colordiffrc (copying /etc/colordiffrc first, if necessary):
Use
colordiff -u file1 file2
for two files orcolordiff -ruN path1 path2
for recursively comparing paths.It's not exactly the same, but it's very close.
这就是我的建议,它非常接近
colordiff
:你必须安装这个brew install colordiff
。在某些 Mac 上移植安装 colordiff
。在 Debian 或 Ubuntu 上 sudo apt-get install colordiff
-R
:这告诉 Less 显示颜色而不是原始代码。我最终使用了
-w
因为我不想看到空格差异。编辑:正如 @Ciprian Tomoiaga 在评论中所建议的,您可以将其设为一个函数并将其也放入您的
~/.bashrc
文件中。This is what I suggest and it's pretty close
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-R
: this tells Less to show colors instead of the raw codes.I ultimately used
-w
because I didn't want to see whitespace diffs.Edit: As suggested by @Ciprian Tomoiaga in the comment, you can make this a function and put it in your
~/.bashrc
file too.根据这个答案在 Unix SE 上。与
-u
一起应该足以模仿git diff
的输出:diff -u --color=always file1 file2 | less -r
--color
在管道中使用时必须always
,auto
将关闭管道中的颜色。我只在 Windows 上使用 Git Bash 尝试过此操作,其中
less -R
只会为块的第一行着色。在这种情况下,less -r
为我修复了它。GNU
diff
has a--color
option since version 3.4 in late 2016 according to this answer on the Unix SE. That alongside-u
should be enough to mimic the output ofgit diff
:diff -u --color=always file1 file2 | less -r
--color
must bealways
when used in a pipe,auto
will turn off color in pipes.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.将其放入 rc 文件中,最常见的是
.bashrc
或.zshrc
:diff() { git diff --no-index "$1" “$2”|颜色差异; }
要求:应已安装
git
和colordiff
。用法:
比较 file1 file2
Place this in your rc file, most commonly it would be either
.bashrc
or.zshrc
:diff() { git diff --no-index "$1" "$2" | colordiff; }
requirements :
git
andcolordiff
should have been installed.usage :
diff file1 file2
仅使用
bash
、diff
、tput
和less
,我们就可以非常接近git 的输出差异
。不过,由于diff
程序员的短视,将会存在一些显着的差异。将以下 Bash 函数定义放入由您的用户帐户自动获取的某个文件中,您将能够从命令行访问该函数:
该函数的工作原理如下:
diff
得到使用各种格式选项调用以指定如何显示文件中的更改。tput
用于将 ANSI 颜色代码插入到这些格式选项中。请注意,使用非 ANSI 终端时,您可能必须将tput setaf
替换为tput setf
。diff
的输出通过管道传输到less
中。-R
允许保留 ANSI 颜色。-X
防止less
在退出时清除屏幕。如果输出适合一个屏幕,-F
会阻止less
作为寻呼机运行。请注意此方法与 git diff 之间的以下差异:
diff
似乎会抱怨并退出。 (至少在 Mac OS X Yosemite 中是这样)。感谢diff
程序员。因此,您可以请求不包含每个更改周围的上下文行(这是默认行为),也可以通过指定@full
作为第一个参数来请求报告文件中所有未更改的行。diff
以更好地处理空格。Using only
bash
,diff
,tput
, andless
, we can closely approximate the output ofgit diff
. There will be some notable differences, though, due to the short-sightedness of thediff
programmers.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:
This function works as follows:
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
.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.@full
, the function will display all unchanged lines in addition to added and removed lines.Note the following differences between this approach and
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.git diff
, the line numbers reported by this function will also vary from those reported bygit diff
.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.由于 bat 具有很好的着色效果,我测试了它是否适用于
diff
令人惊讶的是,它开箱即用,效果非常好。$ diff 文件 1 文件 2 | bat
或$ diff -u file1 file2 |所以
我想你可以创建一个像下面这样的函数来提高效率:
Since bat has nice colorizing, I've tested if that works with
diff
too and surprisingly it worked really well out of the box.$ diff file1 file2 | bat
or$ diff -u file1 file2 | bat
So I suppose you could make a function like this below to be more efficient:
您正在寻找
colordiff
:You are looking for
colordiff
:如果你没有
colordiff
或git diff
,你可以通过vim
获取颜色。或者简单地
If you don't have
colordiff
orgit diff
, you can get color byvim
.or simply
在 debian 9 中测试
diff -u --color=always file1 file2
Tested in debian 9
diff -u --color=always file1 file2
另一种选择是从存储库外部执行此操作,以便 git 知道文件之间的差异。例如。 shell 函数类似于:
The other option is to do it from outside the repository so git knows to diff between files. eg. a shell function something like:
使用colordiff:
安装:
用法:
给出与
git diff
所示完全相同的差异。Use colordiff:
Installation:
Usage:
Gives exactly same difference as shown by
git diff
.添加
到 ~/.zshrc 或 ~/.bashrc
这使用 git diff 在两个文件之间进行普通比较
add
to ~/.zshrc or ~/.bashrc
This uses git diff to do ordinary diff between two files
我认为配置设置:
与“diff”命令的
--relative=
选项相结合可以满足您的需求。你尝试了吗?I think the config setting :
combined with "diff" command's
--relative=<path>
option would do what you wanted. Did you try ?