按更改类型过滤 git diff

发布于 2024-11-26 23:01:48 字数 508 浏览 1 评论 0原文

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

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

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

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

我尝试的另一种选择是:

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

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

Is there a way to limit git diff to changed files?

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:

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. Plus it'd be much nicer if I didn't have to parse (also fails if any hunk contains anything matching /^diff/, for example).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

檐上三寸雪 2024-12-03 23:01:48

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

来自 man git-diff

--diff-filter=[ACDMRTUXB*]

仅选择符合条件的文件

  • A已添加
  • C 已复制
  • D 已删除
  • M 已修改
  • R 已重命名
  • T 更改了其类型(模式)
  • U 未合并
  • X 未知
  • B 的配对已损坏
  • * 全或无

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

*(全有或全无)添加到组合中时,所有路径都会
如果存在符合其他条件的文件,则选择
比较;如果没有与其他条件匹配的文件,则没有任何内容
已选择。

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

You are looking for --diff-filter=M to show only files Modified between the two branches.

From man git-diff

--diff-filter=[ACDMRTUXB*]

Select only files that are

  • A Added
  • C Copied
  • D Deleted
  • M Modified
  • R Renamed
  • T have their type (mode) changed
  • U Unmerged
  • X Unknown
  • B have 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.

Also, these upper-case letters can be downcased to exclude. E.g. --diff-filter=ad excludes added and deleted paths.

心凉 2024-12-03 23:01:48

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

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

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

diff:文档diff-filter排除

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

所以 有关 diff-options 的文档< /code>现在(最终)包括:

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

请务必使用 Git 2.36(2022 年第 2 季度)

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 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)

diff: document diff-filter exclusion

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.

So the documentation on diff-options now (finally) includes:

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

Make sure to use Git 2.36 (Q2 2022).

初懵 2024-12-03 23:01:48

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

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

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

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

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

注意:.gitignore 不在 export.zip

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.

To get an export as zip you can use this code

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

Note: .gitignore is not in export.zip

爱本泡沫多脆弱 2024-12-03 23:01:48

您可以使用 --diff-filter 标志来精确执行此操作。 git diff --diff-filter=CMRTUXB master.. 应该显示除添加/删除的文件之外的所有内容。

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

老街孤人 2024-12-03 23:01:48

我使用 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

唯一的问题是,当它到达终点时。你必须“ctrl+home”并再次进入,直到找不到任何东西。

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

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

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

(Replace whats found with 'Index')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文