How to invert `git log --grep=` or How to show git logs that don't match a pattern

前端 未结 8 886
旧巷少年郎
旧巷少年郎 2020-12-02 22:34

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:

8条回答
  •  一整个雨季
    2020-12-02 23:18

    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.

提交回复
热议问题