取消 git 中已删除文件的差异

我希望快速了解存储库中的本地更改,但是我不希望看到显示已删除文件的 diff,因为每一行都是一个负号。

基本上,我想要类似 'git diff HEAD <list of modified files only>'的东西。在理想情况下,它的前面应该是已删除和添加的文件列表,但不显示其中的差异。

我大部分时间都在编写一个实用程序来完成这个任务:

git diff HEAD `git status | grep modified | cut -d : -f 2`

当我想知道是否有一些 Git-y 的方式来代替它。我是不是漏掉了一面旗子?我也想保留颜色输出。

20212 次浏览

在 Git 版本1.8.5和更新的版本中,您可以使用 --diff-filter选项并指定“ d”(小写)来告诉它排除已删除的文件。

$ git diff --diff-filter=d

在1.8.5以上的 Git 版本中,可以使用 --diff-filter选项并指定除“ D”(已删除)条件以外的所有条件:

$ git diff --diff-filter=ACMRTUXB

对于已删除的文件,git diff -D(或等效的 git diff --irreversible-delete)将省略 diff 主体。我不认为有一个等价的添加文件。

您还可以使用-M,它尝试查找被移动的文件

git diff -M -D

More 可以通过: git diff —— help 获得更多信息(选项 B 也可能很有趣)

答案几乎与发布的 Dan Moulding相同,但你可能想指定你的 不要想显示什么,为了隐藏已删除的文件,它将是:

git diff --diff-filter=d

在前一个答案的基础上,我想添加 git 版本2.33.0的文档说明

--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.


Note that not all diffs can feature all types. For instance, diffs from the index to the working tree can never have Added entries (because the set of paths included in the diff is limited by what is in the index).
Similarly, copied and renamed entries cannot appear if detection for those types is disabled.