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

天涯浪子 提交于 2019-12-20 08:09:08

问题


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 "old" and "new".


回答1:


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.




回答2:


  • Offical 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



回答3:


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.



来源:https://stackoverflow.com/questions/6894322/how-to-make-git-diff-and-git-log-ignore-new-and-deleted-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!