Search code examples
regexgitgit-diff

Git diff: ignore lines starting with a word


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 * 

Solution

  • 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