sergej
sergej

Reputation: 18009

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 * 

Upvotes: 3

Views: 2376

Answers (3)

Dirk Braunschweiger
Dirk Braunschweiger

Reputation: 1

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.

Upvotes: 0

sergej
sergej

Reputation: 18009

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

Upvotes: 5

Myys 3
Myys 3

Reputation: 29

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

Upvotes: 2

Related Questions