开发者

How to make git-diff and git log ignore new and deleted files?

Sometimes there's a couple of changed files together with some new, deleted and/or renamed files. When doing git diff or git-log I'd like to omit them, so I can better spot the modifications.

Actually, listing the names of the new and deleted files without their content would be best. For "old" renamed to "new" I'd like to optionally get the difference between开发者_StackOverflow中文版 "old" and "new".


The --diff-filter option works with both diff and log.

I use --diff-filter=M a lot which restricts diff outputs to only content modifications.

To detect renames and copies and use these in the diff output, you can use -M and -C respectively, together with the R and C options to --diff-filter.


Official document:

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

Example: show only added , changed, modified files exclude deleted files:

git diff --diff-filter=ACM


Also, these upper-case letters can be downcased to exclude.
E.g. --diff-filter=ad excludes added and deleted paths.

In your case, git diff --diff-filter=ad would work, but make sure to not use lower and upper letters in the same filter, unless you have Git 2.36 (Q2 2022).

"git diff --diff-filter=aR"(man) is now parsed correctly.

See commit 75408ca, commit 4d4d4ea, commit d843e31 (28 Jan 2022) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit 9a16099, 16 Feb 2022)

diff-filter: be more careful when looking for negative bits

Signed-off-by: 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.


UPDATE: The accepted answer by Charles Bailey is the correct one; the desired functionality is already built into git.

I'll leave this answer here since it might provide ideas for things that aren't built into git.


git diff shows new and deleted files by comparing them to /dev/null. It shouldn't be too difficult to write something (I'd use Perl myself) that looks for /dev/null and filters out the following lines up to the next diff. Then git diff ... | the-filter.

Renamed files are a different matter; I don't (yet) have a good answer to that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜