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

前端 未结 8 908
旧巷少年郎
旧巷少年郎 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:25

    A relatively simple method with a lot of flexibility is to use git log with the -z option piped to awk. The -z option adds nulls between commit records, and so makes it easy parse with awk:

    git log --color=always -z | awk -v RS=\\0
    

    (color=always is required to keep coloring when the output is a pipe). Then, its simple to add any boolean expression you want that works on each field. For example, this will print all entries where the author email is not from fugly.com, and the day of the commit was Sunday:

    git log --color=always -z | awk -v RS=\\0 '!/Author:.*fugly.com>/ && /Date:.* Sun /'
    

    Another nice thing is its you can add in any formatting option or revision range to the git log, and it still works.

    One last thing, if you want to paginate it, use "less -r" to keep the colors.

    EDIT: changed to use -v in awk to make it a little simpler.

提交回复
热议问题