How to invert `git log --grep=` or How to

2019-01-11 05:49发布

I want to use git log to show all commits that do not match a given pattern. I know I can use the following to show all commits that do match a pattern:

git log --grep=<pattern>

How do I invert the sense of matching?

I am trying to ignore commits that have "bumped to version ..." in the message.

EDIT: I want my final output to be pretty verbose. e.g. git log --pretty --stat. So output from git log --format=oneline won't work for me.

标签: git git-log
8条回答
Juvenile、少年°
2楼-- · 2019-01-11 06:41

As with thebriguy's answer, grep also has a -z option to enable it to work with null terminated strings rather than lines. This would then be as simple as inverting the match:

git log -z --color | grep -vz "bumped to version"

For safety you may want to match within the commit message only. To do this with grep, you need to use pearl expressions to match newlines within the null terminated strings. To skip the header:

git log -z | grep -Pvz '^commit.*\nAuthor:.*\nDate:.*\n[\S\s]*bumped to version'

Or with colour:

git log -z --color | \
  grep -Pvz '^.....commit.*\nAuthor:.*\nDate:.*\n[\S\s]*bumped to version'

Finally, if using --stat, you could also match the beginning of this output to avoid matching file names containing the commit string. So a full answer to the question would look like:

log -z --color --pretty --stat | \
  grep -Pvz '^.....commit.*\nAuthor:.*\nDate:.*\n[\S\s]*?bumped to version[\S\s]*?\n [^ ]'

Note that grep -P is described as 'highly experimental' in my man page. It may be better to use pcregrep instead which uses libpcre, see How to give a pattern for new line in grep?. Although grep -P works fine for me and I have no idea if pcregrep has a -z option or equivalent.

查看更多
混吃等死
3楼-- · 2019-01-11 06:44

get a list of all commits containing your result, then filter out their hashes.

git log --format=oneline | grep -v `git log --grep="bumped to version" --format="%h"`
查看更多
登录 后发表回答