Git diff:仅显示未匹配模式的更改

5

有没有办法告诉git diff假定以某种模式开头的行未更改?

例如,考虑以下内容:

$ git diff -U0
diff --git a/file_a.txt b/file_a.txt
index 26ed843..4071ff8 100644
--- a/file_a.txt
+++ b/file_a.txt
@@ -24 +24 @@
- * unimportant foo
+ * unimportant bar
diff --git a/file_b.txt b/file_b.txt
index c6d051e..4b3cf22 100644
--- a/file_b.txt
+++ b/file_b.txt
@@ -24 +24 @@
- * unimportant foo
+ * unimportant bar
@@ -48,0 +49 @@
+   this is important  
@@ -56,0 +58 @@
+   this is also important

以星号开头的行(正则表达式模式为"^[[:space:]]*\*.*")不重要,我希望从git diff的输出中仅筛选包含此类行更改的文件。在上面的示例中,输出应仅报告对file_b.txt的更改。是否可能?

1个回答

5
使用git diff -G命令并反转正则表达式,匹配行的第一个非空字符既不是*也不是空格。
-G '^[[:space:]]*[^[:space:]*]'

这不是很高效,因为会回溯,但似乎不支持负预测'^(?!\s*\*)',占有量词'^\s*+[^*]'或原子组'^(?>\s*)[^*]'


5
这可能是目前最好的方法,但请注意其局限性。grep应用于差异而非行,因此与重要更改相邻的不重要更改通常不会被过滤掉。 - Mark Adelsberger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接