Filter git diff by type of change
有没有一种方法可以将
我想查看两次提交之间的差异,但排除一个或另一个不存在的路径(添加/删除)。 以下Perl一线式说明了我想要的大部分内容:
1 | git diff master.. | perl -lnwe 'print unless /^(new|deleted) file/../^diff/ and not /^diff/' |
但是,对于新文件或已删除文件,剩下
我尝试过的另一种选择是:
1 | git diff --name-status (args) | perl -lnwe 'print if s/^M\s+//' | xargs git diff (args) -- |
它的输出更好,但是仍然让人感到有些呆滞。
您正在寻找
来自
--diff-filter=[ACDMRTUXB*] Select only files that are
A AddedC CopiedD DeletedM ModifiedR RenamedT have their type (mode) changedU UnmergedX UnknownB have had their pairing Broken* All-or-noneAny 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.
正如Git 2.10(2016年第三季度)会提醒我们的那样,有一种更简便的方法"显示除已添加/删除的文件外的所有内容"。 (实际上自2013年7月Git 1.8.5起)
1 | git diff --diff-filter=ad master.. |
参见Junio C Hamano(
(由Junio C Hamano-
diff : documentdiff-filter exclusionIn 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.
因此,
These upper-case letters can be downcased to exclude.
E.g.--diff-filter=ad excludes added and deleted paths.
您可以使用--diff-filter标志来精确地做到这一点。
要查看所有修改过的文件和新文件,您可以使用
1 | git diff --name-only --diff-filter=ACMR PREV_VERSION master |
要以zip格式导出,可以使用以下代码
1 | git archive --output=export.zip HEAD $(git diff --name-only --diff-filter=ACMR PREV_VERSION HEAD) |
注意:
我使用了Notepad ++(Windows)和这些正则表达式来过滤扩展文件类型和diff文件中的某些路径。
1 2 3 | ^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 |
唯一的问题是,到达终点时。您必须先按" ctrl + home",然后再次查找,直到找不到任何内容。
(用"索引"替换发现的内容)