5

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?

sergej
  • 17,147
  • 6
  • 52
  • 89

1 Answers1

5

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.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • 5
    This may be about the best approach there is, but be aware of its limitations. The grep applies to the difference rather than the line, so unimportant changes adjacent to important changes will not (generally) be filtered out. – Mark Adelsberger Nov 23 '18 at 17:06