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

时间:2018-11-23 16:31:59

标签: regex git git-diff

是否可以让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 个答案:

答案 0 :(得分:4)

可以使用git diff -G标志并反转正则表达式以匹配行,即第一个非空格字符既不是*也不是空格。

-G '^[[:space:]]*[^[:space:]*]'

效率不高,因为会回溯,但似乎不支持负前瞻'^(?!\s*\*)',所有格量词'^\s*+[^*]'或原子组'^(?>\s*)[^*]'