如何使 git-diff 和 git log 忽略新文件和已删除文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6894322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:27:44  来源:igfitidea点击:

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

gitgit-diff

提问by maaartinus

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

有时会有几个更改的文件以及一些新的、删除的和/或重命名的文件。在做git diffgit-log我想省略它们时,我可以更好地发现修改。

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

实际上,最好列出没有内容的新文件和已删除文件的名称。对于重命名为“新”的“旧”,我想选择性地获得“旧”和“新”之间的区别。

回答by CB Bailey

The --diff-filteroption works with both diffand log.

--diff-filter选项适用于diff和日志。

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

我使用--diff-filter=M了很多将差异输出限制为仅内容修改的方法。

To detect renames and copies and use these in the diff output, you can use -Mand -Crespectively, together with the Rand Coptions to --diff-filter.

要检测重命名和副本并在 diff 输出中使用它们,您可以分别使用-M和以及 和选项。-CRC--diff-filter

回答by TeeTracker

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

--diff-filter=[(A|C|D|M|R|T|U|X|B)…?[*]]

仅选择已添加 (A)、已复制 (C)、已删除 (D)、已修改 (M)、已重命名 (R)、其类型(即常规文件、符号链接、子模块……?)已更改 (T) 的文件,未合并 (U)、未知 (X) 或配对已损坏 (B)。可以使用过滤器字符的任意组合(包括无)。

当在组合中添加*(All-or-none)时,如果在比较中存在匹配其他条件的文件,则选择所有路径;如果没有符合其他条件的文件,则不选择任何内容。

此外,可以将这些大写字母小写以排除。例如 --diff-filter=ad 排除添加和删除的路径。

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

示例:仅显示已添加、已更改、已修改的文件,排除已删除的文件:

git diff --diff-filter=ACM

回答by Keith Thompson

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

更新:接受的答案由查尔斯·贝利是正确的; 所需的功能已经内置到 git 中。

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

我将把这个答案留在这里,因为它可能会为 git 没有内置的东西提供想法。



git diffshows 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/nulland filters out the following lines up to the next diff. Then git diff ... | the-filter.

git diff通过将新文件和已删除文件与/dev/null. 编写一些东西(我自己会使用 Perl)来查找/dev/null并过滤掉以下行直到下一个差异应该不会太难。然后git diff ... | the-filter

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

重命名的文件是另一回事;我(还)对此没有很好的答案。