3

As I have learned here, we can tell git diff to ignore lines starting with a * using:

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

How do I tell git to ignore lines starting with a word, or more (for example: * Generated at), not just a character?

This file shall be ignored, it contains only trivial changes:

- * Generated at 2018-11-21
+ * Generated at 2018-11-23

This file shall NOT be ignored, it contains NOT only trivial changes:

- * Generated at 2018-11-21
+ * Generated at 2018-11-23
+ * This line is important! Although it starts with a * 
sergej
  • 17,147
  • 6
  • 52
  • 89
  • Probably a dupe of [ignoring changes matching a string in git diff](https://stackoverflow.com/questions/15878622/ignoring-changes-matching-a-string-in-git-diff) – Wiktor Stribiżew Nov 23 '18 at 18:30

3 Answers3

5

Git is using POSIX regular expressions which seem not to support lookarounds. That is the reason why @Myys 3's approach does not work. A not so elegant workaround could be something like this:

git diff -G '^\s*([^\s*]|\*\s*[^\sG]|\*\sG[^e]|\*\sGe[^n]|\*\sGen[^e]|\*\sGene[^r]|\*\sGener[^a]|\*\sGenera[^t]|\*\sGenerat[^e]|\*\sGenerate[^d]).*'

This will filter out all changes starting with "* Generated".

Test: https://regex101.com/r/kdv4V0/3

sergej
  • 17,147
  • 6
  • 52
  • 89
3

Considering you are ignoring changes that does NOT match your regex, you just have to put the words you want inside the expression within a lookahead capture group, like this:

git diff -G '^(?=.*Generated at)[[:space:]]*[^[:space:]*]'

Note that if you want to keep adding words to ignore, just keep adding these groups (don't forget the .*): However, if the string contains a "Generated at" anywhere in their whole, it shall be ignored. If you want to define exactly how it should start, then replace the . with a [^[:word:]].

git diff -G '^(?=[^[:word:]]*Generated at)[[:space:]]*[^[:space:]*]'

You can have a look at it's behaviour at

Version 1: .*

https://regex101.com/r/kdv4V0/1

Version 2: [^[:word:]]*

https://regex101.com/r/kdv4V0/2

Myys 3
  • 31
  • 3
  • 4
    Thanks for your answer. However, git version 2.7.4 seems not to support this regex, I am getting `fatal: invalid regex: Invalid preceding regular expression`. – sergej Nov 24 '18 at 10:20
0

TL;DR: git diff -G is not able to exclude changes only to include changes that match the regex.

Have a look at git diff: ignore deletion or insertion of certain regex There torek explains how git log and git diff work and how the parameter -G works.