按更改类型过滤 git diff

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6879501/
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:26:57  来源:igfitidea点击:

Filter git diff by type of change

gitdiff

提问by benizi

Is there a way to limit git diffto changed files?

有没有办法限制git diff更改的文件?

I'd like to see the differences between two commits, but exclude paths that don't exist in one or the other (additions/deletions). The following Perl one-liner illustrates most of what I want:

我想看看两个提交之间的差异,但排除一个或另一个中不存在的路径(添加/删除)。以下 Perl 单行代码说明了我想要的大部分内容:

git diff master.. | perl -lnwe 'print unless /^(new|deleted) file/../^diff/ and not /^diff/'

But that leaves diff --git a/path b/pathlines for the files that were new or deleted. Plus it'd be much nicer if I didn't have to parse (also fails if any hunk contains anything matching /^diff/, for example).

但这留下diff --git a/path b/path了新文件或删除文件的行。另外,如果我不必解析它会更好(例如,如果任何大块包含匹配 /^diff/ 的任何内容也会失败)。

Another alternative I tried was:

我尝试的另一种选择是:

git diff --name-status (args) | perl -lnwe 'print if s/^M\s+//' | xargs git diff (args) --

Its output is better, but it still feels hackish.

它的输出更好,但仍然感觉很hackish。

回答by zen

You are looking for --diff-filter=Mto show only files *M*odified between the two branches.

您正在寻找--diff-filter=M仅显示两个分支之间的* M*odified文件。

From man git-diff

man git-diff

--diff-filter=[ACDMRTUXB*]

Select only files that are

  • AAdded
  • CCopied
  • DDeleted
  • MModified
  • RRenamed
  • Thave their type (mode) changed
  • UUnmerged
  • XUnknown
  • Bhave had their pairing Broken
  • *All-or-none

Any combination of the filter characters may 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.

--diff-filter=[ACDMRTUXB*]

仅选择符合以下条件的文件

  • A添加
  • C已复制
  • D已删除
  • M修改的
  • R重命名
  • T改变他们的类型(模式)
  • U未合并
  • X未知
  • B已经有他们的配对破碎
  • *全有或全无

可以使用过滤器字符的任何组合。

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

回答by VonC

As Git 2.10 (Q3 2016) will remind us, there is an easier way to "show everything except added/deleted files." (actually since Git 1.8.5, July 2013)

正如 Git 2.10(2016 年第 3 季度)将提醒我们的那样,有一种更简单的方法可以“显示除添加/删除的文件之外的所有内容”。(实际上从 Git 1.8.5 开始,2013 年 7 月)

 git diff --diff-filter=ad master..

See commit 16726cf(14 Jul 2016) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster--in commit 2f8c654, 08 Aug 2016)

请参阅Junio C Hamano() 的commit 16726cf(2016 年 7 月 14 日(由Junio C Hamano合并-- --commit 2f8c654,2016 年 8 月 8 日)gitster
gitster

diff: document diff-filterexclusion

In v1.8.5 days, 7f2ea5f (diff: allow lowercase letter to specify what change class to exclude, 2013-07-17)taught the "--diff-filter" mechanism to take lowercase letters as exclusion, but we forgot to document it.

diff: 文件diff-filter排除

在 v1.8.5 天,7f2ea5f(diff:允许小写字母指定要排除的更改类,2013-07-17)教导“ --diff-filter”机制将小写字母作为排除,但我们忘记记录它。

So the documentation on diff-optionsnow (finally) includes:

所以现在(最终)的文档diff-options包括:

These upper-case letters can be downcased to exclude.
E.g. --diff-filter=adexcludes added and deleted paths.

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

回答by Lily Ballard

You can use the --diff-filter flag to do precisely this. git diff --diff-filter=CMRTUXB master..should show everything except added/deleted files.

您可以使用 --diff-filter 标志来精确地做到这一点。git diff --diff-filter=CMRTUXB master..应该显示除添加/删除文件之外的所有内容。

回答by iwg

To see all modified and new files you can use

要查看您可以使用的所有修改过的和新的文件

git diff --name-only --diff-filter=ACMR PREV_VERSION master

PREV_VERSIONis the hash of your first commit.

PREV_VERSION是你第一次提交的哈希值。

To get an export as zip you can use this code

要导出为 zip,您可以使用此代码

git archive --output=export.zip HEAD $(git diff --name-only --diff-filter=ACMR PREV_VERSION HEAD)

Note: .gitignoreis not in export.zip

注意:.gitignore不在export.zip

回答by Krzysztof Stankiewicz

I've used Notepad++ (Windows), and these regular expressions to filter out extension types and certain paths from a diff file.

我已经使用 Notepad++ (Windows) 和这些正则表达式从 diff 文件中过滤掉扩展类型和某些路径。

^Index.*\.(dll|pdb|exe|txt|zip|log|ism|resx|tlog|htm|lib)$[\s\S.]*?^Index
^Index: Shared/.+$[\s\S.]*?^Index
^Index: Next/source/Utility/.+$[\s\S.]*?^Index

Only problem is, when it reaches the end. You have to 'ctrl+home' and go again until it finds nothing.

唯一的问题是,当它到达终点时。你必须'ctrl+home'然后再去,直到它什么也没找到。

(Replace whats found with 'Index')

(用“索引”替换找到的内容)