在git中抑制已删除文件的差异。

85
我想快速了解本地代码库的变更情况,但我不想看到已删除文件的差异,因为每一行都是减号。
基本上,我需要像这样的东西<'git diff HEAD <list of modified files only>'>。在理想情况下,它会先显示已删除和已添加文件的列表,但不会显示它们内部的差异。
我已经写了一个几乎能实现这个功能的工具:
git diff HEAD `git status | grep modified | cut -d : -f 2`

当我在思考是否有一些git-y的方式来代替它时,是否有我遗漏的标志?我也希望保留颜色输出。

5个回答

128

在 Git 版本 1.8.5 及更高版本中,您可以使用 --diff-filter 选项,并指定 "d"(小写字母)来排除已删除的文件。

$ git diff --diff-filter=d

在 Git 版本早于 1.8.5 的版本中,您可以使用 --diff-filter 选项,并指定除 "D"(已删除)标准以外的所有标准:

$ git diff --diff-filter=ACMRTUXB

16
--diff-filter=M 表示只显示修改过的内容。 - jackocnr
11
或者只需使用 git diff --diff-filter=d 命令。 - Pithikos
4
--diff-filter 的文档:https://git-scm.com/docs/git-diff#git-diff---diff-filterACDMRTUXB82308203 - noitseuq
感谢您明确说明了与Git版本相关的不同行为,我刚刚遇到了V1.7版本的问题,这对我帮助很大! - Astyan
好像有点过分,两年后抄袭@nktssh的答案。 - undefined

33

git diff -D(或等效于git diff --irreversible-delete)将省略已删除文件的差异部分。 我认为没有对于添加文件的等价选项。


1
与其他选项不同,此选项保留了摘要 diff --git a/ ... deleted file mode 100644 ... index ...。 - Oleh Prypin

27

Dan Moulding 发布的答案几乎相同,但您可能需要指定您不想显示的内容,对于隐藏已删除的文件,应为:

git diff --diff-filter=d

2
您也可以使用-M选项,尝试查找已移动的文件。
git diff -M -D 

您可以通过执行 git diff --help 命令(选项 -B 也可能很有用)来获取更多信息。


0

除了之前的回答,我想补充一下关于 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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接