Suppressing diffs for deleted files in git
Asked Answered
H

5

87

I want to get a quick overview of the local changes in my repository, but I don't want a diff that shows deleted files, since every single line is a minus.

Basically, I want something like 'git diff HEAD <list of modified files only>'. In an ideal world, it would be preceded by the list of deleted and added files, but not show the diffs within them.

I was most of the way through writing a utility that does this:

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

when I wondered if there was some git-y way to do it instead. Is there a flag I'm missing? I'd love to preserve the color output, too.

Hutson answered 11/9, 2010 at 18:35 Comment(0)
F
133

In Git versions 1.8.5 and newer, you can do this using the --diff-filter option and specifying "d" (lowercase) to tell it to exclude deleted files.

$ git diff --diff-filter=d

In Git versions older than 1.8.5, you can do this with the --diff-filter option and specifying all but the "D" (deleted) criteria:

$ git diff --diff-filter=ACMRTUXB

For reference the git documentation of version 2.43.2 says:

--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, copied and renamed entries cannot appear if detection for those types is disabled.

Forgiven answered 11/9, 2010 at 18:57 Comment(6)
--diff-filter=M for just modificationsBitner
or just git diff --diff-filter=dCoycoyle
Docs for --diff-filter: git-scm.com/docs/…Ferocious
Thanks for precising the different behaviors linked to the version of Git, I just had the issue with thhe version V1.7, and this helped much !Jabot
Seems a bit harsh to copy in @nktssh's answer over two years later.Brunner
@c_z It is the correct thing to do in SO to get a better answer. Answers are in effect wikis and not owned by the original author.Fingerstall
B
36

git diff -D (or equivalently git diff --irreversible-delete) will omit the diff body for deleted files. I don't think there's an equivalent for added files.

Baseboard answered 13/3, 2013 at 1:6 Comment(2)
This option, unlike the other one, keeps the summary diff --git a/ ... deleted file mode 100644 ... index ...Crosspurpose
This is useful, but beware that the resulting diff no longer applies cleanly with patch -p1: "Not deleting file […] as content differs from patch".Cowardice
C
28

Almost same answer as posted Dan Moulding, but you probably want to specify what you don't want to show, and for hide deleted files it will be:

git diff --diff-filter=d
Cubitiere answered 20/1, 2017 at 21:32 Comment(0)
I
2

You also may use -M which try to find files that was moved

git diff -M -D 

more may get more info with: git diff --help (option -B also could be interesting)

Intercessor answered 10/11, 2016 at 7:12 Comment(0)
V
0

On top of the previous answer, I'd like to add what the documentation says for git version 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.
Vapid answered 16/9, 2021 at 22:22 Comment(1)
This is better as an edit to Dan Mouldings's accepted answerFingerstall

© 2022 - 2024 — McMap. All rights reserved.