问题
Is it possible to tell git diff
to assume lines staring with some pattern as unchanged?
For example, consider the following:
$ 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
Lines starting with an asterisk (regex pattern "^[[:space:]]*\*.*"
) are not important and I would like to filter files that contain changes in such lines only from the output of git diff
. In the example above, the output should report file_b.txt
changes only. Is it possible?
回答1:
It's possible with the git diff -G
flag and making inverting the regexp to match lines that the first character that is not space is neither a *
nor space.
-G '^[[:space:]]*[^[:space:]*]'
Not very efficient because will backtrack but seems negative lookahead '^(?!\s*\*)'
, possessive quantifier '^\s*+[^*]'
or atomic groups '^(?>\s*)[^*]'
are not supported.
来源:https://stackoverflow.com/questions/53450186/git-diff-show-only-changes-not-matching-a-pattern