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

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

    This will be possible with Git 2.4+ (Q2 2015): see commit 22dfa8a by Christoph Junghans (junghans):

    log: teach --invert-grep option

    "git log --grep=" shows only commits with messages that match the given string, but sometimes it is useful to be able to show only commits that do not have certain messages (e.g. "show me ones that are not FIXUP commits").

    Originally, we had the invert-grep flag in grep_opt, but because "git grep --invert-grep" does not make sense except in conjunction with "--files-with-matches", which is already covered by "--files-without-matches", it was moved it to revisions structure.
    To have the flag there expresses the function to the feature better.

    When the newly inserted two tests run, the history would have commits with messages "initial", "second", "third", "fourth", "fifth", "sixth" and "Second", committed in this order.
    The commits that does not match either "th" or "Sec" is "second" and "initial". For the case insensitive case only "initial" matches.

    --invert-grep
    

    Limit the commits output to ones with log message that do not match the pattern specified with --grep=.

    Example:

    I first grep message with "sequencer" in them:

    vonc@voncm C:\Users\vonc\prog\git\git
    
    > git log -2 --pretty="tformat:%s" --grep=sequencer
    Merge branch 'js/sequencer-wo-die'
    sequencer: ensure to release the lock when we could not read the index
    

    If I want messages with no sequencer:

    > git log -2 --pretty="tformat:%s" --grep=sequencer --invert-grep
    Second batch for 2.11
    Merge branch 'js/git-gui-commit-gpgsign'
    

提交回复
热议问题