简体   繁体   English

按类型过滤git diff

[英]Filter git diff by type of change

Is there a way to limit git diff to 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/path lines for the files that were new or deleted. 但是,这会为新文件或删除的文件留下diff --git a/path b/path行。 Plus it'd be much nicer if I didn't have to parse (also fails if any hunk contains anything matching /^diff/, for example). 另外,如果我不需要解析它会更好(如果任何hunk包含匹配/ ^ 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. 它的输出更好,但仍然感觉很乱。

You are looking for --diff-filter=M to show only files * M *odified between the two branches. 您正在寻找--diff-filter=M以仅显示两个分支之间的文件* M *。

From man git-diff 来自man git-diff

--diff-filter=[ACDMRTUXB*]

Select only files that are 仅选择文件

  • A Added A补充
  • C Copied C复制
  • D Deleted D已删除
  • M Modified M修改
  • R Renamed R改名
  • T have their type (mode) changed T的类型(模式)已更改
  • U Unmerged U Unmerged
  • X Unknown X未知
  • B have had their pairing Broken B已配对破碎了
  • * 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. 如果没有与其他条件匹配的文件,则不会选择任何内容。

As Git 2.10 (Q3 2016) will remind us, there is an easier way to "show everything except added/deleted files." 正如Git 2.10(2016年第3季度)将提醒我们的,有一种更简单的方法来“显示除添加/删除文件之外的所有内容”。 (actually since Git 1.8.5, July 2013) (实际上自Git 1.8.5,2013年7月)

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

See commit 16726cf (14 Jul 2016) by Junio C Hamano ( gitster ) . Junio C gitstergitster 提交的16726cf (2016年7月14日
(Merged by Junio C Hamano -- gitster -- in commit 2f8c654 , 08 Aug 2016) (由Junio C gitster合并- gitster - in commit 2f8c654 ,2016年8月8日)

diff : document diff-filter exclusion diff :文档diff-filter排除

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

So the documentation on diff-options now (finally) includes: 所以关于diff-options的文档现在(最后)包括:

These upper-case letters can be downcased to exclude. 这些大写字母可以降级为排除。
Eg --diff-filter=ad excludes added and deleted paths. 例如--diff-filter=ad排除添加和删除的路径。

You can use the --diff-filter flag to do precisely this. 您可以使用--diff-filter标志来完成此操作。 git diff --diff-filter=CMRTUXB master.. should show everything except added/deleted files. git diff --diff-filter=CMRTUXB master..应该显示除添加/删除文件之外的所有内容。

To see all modified and new files you can use 要查看您可以使用的所有已修改和新文件

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

PREV_VERSION is 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: .gitignore is not in export.zip 注意: .gitignore不在export.zip

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') (替换“索引”中找到的内容)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM