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

ser*_*gej 6 regex git git-diff

是否可以假设git diff以某种模式开头的行保持不变?

例如,请考虑以下情况:

git diff -U0
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

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

Nah*_*eul 5

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

-G '^[[:space:]]*[^[:space:]*]'
Run Code Online (Sandbox Code Playgroud)

它不是很有效,因为它会回溯,但似乎不支持否定前瞻'^(?!\s*\*)'、所有格量词'^\s*+[^*]'或原子组。'^(?>\s*)[^*]'

  • 这可能是最好的方法,但要注意它的局限性。grep 适用于差异而不是行,因此与重要更改相邻的不重要更改(通常)不会被过滤掉。 (5认同)