Git diff:忽略以单词开头的行
Git diff: ignore lines starting with a word
据我所知 ,我们可以告诉 git diff
忽略以 *
开头的行,使用:
git diff -G '^[[:space:]]*[^[:space:]*]'
如何让 git 忽略以一个或多个单词(例如:* Generated at
)开头的行,而不仅仅是一个字符?
应忽略此文件,它仅包含微不足道的更改:
- * Generated at 2018-11-21
+ * Generated at 2018-11-23
不应忽略此文件,它不仅包含微不足道的更改:
- * Generated at 2018-11-21
+ * Generated at 2018-11-23
+ * This line is important! Although it starts with a *
考虑到您忽略了 NOT 匹配您的正则表达式的更改,您只需将您想要的单词放在前瞻捕获组中的表达式中,如下所示:
git diff -G '^(?=.*Generated at)[[:space:]]*[^[:space:]*]'
请注意,如果您想继续添加要忽略的词,只需继续添加这些组即可(不要忘记 .*
):
但是,如果整个字符串中包含 "Generated at" anywhere,则忽略该字符串。如果您想准确定义它应该如何开始,请将 .
替换为 [^[:word:]]
.
git diff -G '^(?=[^[:word:]]*Generated at)[[:space:]]*[^[:space:]*]'
您可以在
查看它的行为
版本 1: .*
https://regex101.com/r/kdv4V0/1
版本 2: [^[:word:]]*
Git 正在使用 POSIX regular expressions which seem not to support lookarounds. That is the reason why 不起作用。一个不太优雅的解决方法可能是这样的:
git diff -G '^\s*([^\s*]|\*\s*[^\sG]|\*\sG[^e]|\*\sGe[^n]|\*\sGen[^e]|\*\sGene[^r]|\*\sGener[^a]|\*\sGenera[^t]|\*\sGenerat[^e]|\*\sGenerate[^d]).*'
这将过滤掉所有以“* Generated
”开头的更改。
TL;DR: git diff -G 无法排除仅包含与正则表达式匹配的更改的更改。
看看
那里解释了 git log
和 git diff
的工作原理以及参数 -G
的工作原理。
据我所知 git diff
忽略以 *
开头的行,使用:
git diff -G '^[[:space:]]*[^[:space:]*]'
如何让 git 忽略以一个或多个单词(例如:* Generated at
)开头的行,而不仅仅是一个字符?
应忽略此文件,它仅包含微不足道的更改:
- * Generated at 2018-11-21
+ * Generated at 2018-11-23
不应忽略此文件,它不仅包含微不足道的更改:
- * Generated at 2018-11-21
+ * Generated at 2018-11-23
+ * This line is important! Although it starts with a *
考虑到您忽略了 NOT 匹配您的正则表达式的更改,您只需将您想要的单词放在前瞻捕获组中的表达式中,如下所示:
git diff -G '^(?=.*Generated at)[[:space:]]*[^[:space:]*]'
请注意,如果您想继续添加要忽略的词,只需继续添加这些组即可(不要忘记 .*
):
但是,如果整个字符串中包含 "Generated at" anywhere,则忽略该字符串。如果您想准确定义它应该如何开始,请将 .
替换为 [^[:word:]]
.
git diff -G '^(?=[^[:word:]]*Generated at)[[:space:]]*[^[:space:]*]'
您可以在
查看它的行为版本 1: .*
https://regex101.com/r/kdv4V0/1
版本 2: [^[:word:]]*
Git 正在使用 POSIX regular expressions which seem not to support lookarounds. That is the reason why
git diff -G '^\s*([^\s*]|\*\s*[^\sG]|\*\sG[^e]|\*\sGe[^n]|\*\sGen[^e]|\*\sGene[^r]|\*\sGener[^a]|\*\sGenera[^t]|\*\sGenerat[^e]|\*\sGenerate[^d]).*'
这将过滤掉所有以“* Generated
”开头的更改。
TL;DR: git diff -G 无法排除仅包含与正则表达式匹配的更改的更改。
看看git log
和 git diff
的工作原理以及参数 -G
的工作原理。