Git diff: ignore lines starting with a word

寵の児 提交于 2019-12-11 11:26:49

问题


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 * 

回答1:


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/53451455/git-diff-ignore-lines-starting-with-a-word

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!