有时会有一些更改过的文件以及一些新的、删除的和/或重命名的文件。在执行git diff或git-log时,我想省略它们,这样我就可以更好地发现修改。

实际上,列出新文件和删除文件的名称而不列出它们的内容是最好的。对于“旧”重命名为“新”,我想选择获得“旧”和“新”之间的差异。


更新:查尔斯·贝利的答案是正确的;所需的功能已经内置到git中。

我将把这个答案留在这里,因为它可能为git中没有内置的东西提供一些想法。


Git diff通过比较/dev/null来显示新建和删除的文件写一些东西(我自己用Perl)来查找/dev/null并过滤掉下面的行直到下一个diff,然后git diff…|过滤器。

重命名文件是另一回事;我(还)没有一个很好的答案。


——diff-filter选项同时适用于diff和log。

我经常使用——diff-filter=M,这将diff输出限制为仅对内容进行修改。

要检测重命名和副本并在diff输出中使用它们,您可以分别使用-M和-C,以及R和C选项来——diff-filter。


官方文档:

--diff-filter=[(A|C|D|M|R|T|U|X|B)…​[*]] Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (i.e. regular file, symlink, submodule, …​) changed (T), are Unmerged (U), are Unknown (X), or have had their pairing Broken (B). Any combination of the filter characters (including none) can be used. When * (All-or-none) is added to the combination, all paths are selected if there is any file that matches other criteria in the comparison; if there is no file that matches other criteria, nothing is selected. Also, these upper-case letters can be downcased to exclude. E.g. --diff-filter=ad excludes added and deleted paths.

示例:只显示添加、修改、修改的文件,不显示删除的文件:

git diff --diff-filter=ACM

此外,可以将这些大写字母小写以排除在外。 例如——diff-filter=ad排除添加和删除的路径。

在你的情况下,git diff——diff-filter=ad可以工作,但请确保不要在同一个过滤器中使用小写字母和大写字母,除非你有git 2.36 (Q2 2022)。

"git diff——diff-filter=aR"(man)现在正确解析。

参见Johannes Schindelin (dscho)的commit 75408ca, commit 4d4d4ea, commit d843e31(2022年1月28日)。 (由Junio C Hamano合并- gitster -在提交9a16099, 2022年2月16日)

差分滤波器:在寻找负位时要更加小心 署名:Johannes Schindelin

The --diff-filter=<bits> option allows to filter the diff by certain criteria, for example R to only show renamed files. It also supports negating a filter via a down-cased letter, i.e. r to show everything but renamed files. However, the code is a bit overzealous when trying to figure out whether git diff(man) should start with all diff-filters turned on because the user provided a lower-case letter: if the --diff-filter argument starts with an upper-case letter, we must not start with all bits turned on. Even worse, it is possible to specify the diff filters in multiple, separate options, e.g. --diff-filter=AM [...] --diff-filter=m. Let's accumulate the include/exclude filters independently, and only special-case the "only exclude filters were specified" case after parsing the options altogether.