Marina Marina - 22 days ago 13
Git Question

how to search git log without a certain commit

Assume I have a branch master with 3 commits, the comments are t123, b1 and b12 separately.

* b90b03f (HEAD -> master) b12
* 27f7577 b1
* 7268b40 t123


And now, I want to use
git log --grep <regex>
to search the log log without t123.
The result I want is

* b90b03f b12
* 27f7577 b1


So how do I use regex to meet the requirement?

Answer

It sounds like what you want is to include all commits whose commit messages do not match some pattern, but --grep includes commits that do match some pattern. But the answer to "how do I write a regexp that matches everything except some pattern" is: You don't.1

You don't need to, because you can use something else (or more precisely, something additional) to exclude the commit with the string "t123" in it. Specifically, if you look at the documentation for git log, you will find that it not only has its --grep=<pattern> option, but also an --invert-grep option:

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

That is, instead of inventing some sort of inverse regular expression, you simply tell the command to invert the result from searching for the regular expression. Since your regexp is just a fixed string with no meta-characters in it:

git log --grep t123 --invert-grep

will do the job. (The = between --grep and the <pattern> part is optional for --grep.)


1It is, in some sense, not impossible; it's just way too difficult, inefficient, and most of all, unnecessary.

Comments