How to invert `git log --grep=<pattern>` or How to show git logs that don't match a pattern
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=<string>
" 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 ingrep_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=<pattern>
.
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'
Warning: this inverts only patterns specified by --grep
.
It will not invert anymore header matches, like --author
, --committer
, starting Git 2.35+ (Q1 2022).
See an example in "equivalence of: git log --exclude-author?
".
Generate a list of all commits, subtract those whose log messages contain the offending pattern, and feed the result to git log
with your desired options. In the final stage, a couple of options to git log
are handy:
--stdin
In addition to the commit listed on the command line, read them from the standard input.
--no-walk
Only show the given revs, but do not traverse their ancestors.
You can do it with a single pipeline and process substitution.
#! /bin/bash
if (( $# < 1 )); then
echo >&2 "Usage: $0 pattern [<since>..<until>]"
exit 1
fi
pattern=$1
shift
git log --format=%H $@ |
grep -v -f <(git log --format=%H "--grep=$pattern" $@) |
git log --pretty --stat --stdin --no-walk
If you don't want to use bash, you could do it with Perl.
#! /usr/bin/env perl
use strict;
use warnings;
no warnings "exec";
sub usage { "Usage: $0 pattern\n" }
sub commits_to_omit {
my($pattern) = @_;
open my $fh, "-|", "git", "log", "--grep=$pattern", "--format=%H", @ARGV
or die "$0: exec: $!";
my %omit = map +($_ => 1), <$fh>;
%omit;
}
die usage unless @ARGV >= 1;
my $pattern = shift;
my %omit = commits_to_omit $pattern;
open my $all, "-|", "git", "log", "--format=%H", @ARGV
or die "$0: exec: $!";
open my $out, "|-", "git", "log", "--pretty", "--stat", "--stdin", "--no-walk"
or die "$0: exec: $!";
while (<$all>) {
print $out $_ unless $omit{$_};
}
Assuming one of the above is in your PATH as git-log-vgrep
and with a history of the form
$ git lola * b0f2a28 (tmp, feature1) D * 68f87b0 C * d311c65 B * a092126 A | * 83052e6 (HEAD, origin/master, master) Z | * 90c3d28 Y | * 4165a42 X | * 37844cb W |/ * f8ba9ea V
we could say
$ git log-vgrep X
to get Z, Y, W, and V.
You can also log other branches, so
$ git log-vgrep A tmp
gives D, C, B, and V; and
$ git log-vgrep C tmp~2..tmp
yields just D.
One limitation of the above implementations is if you use a pattern that matches all commits, e.g., .
or ^
, then you'll get HEAD. This is how git log
works:
$ git log --stdin --no-walk --pretty=oneline </dev/null 83052e62f0dc1c6ddfc1aff3463504a4bf23e3c4 Z
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.
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"`
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.