If you want to find all commits where the commit message contains a given word, use
$ git log --grep=word
If you want to find all commits where "word" was added or removed in the file contents (to be more exact: where the number of occurrences of "word" changed), i.e., search the commit contents, use a so-called 'pickaxe' search with
$ git log -Sword
In modern Git there is also
$ git log -Gword
to look for differences whose added or removed line matches "word" (also commit contents).
Note that -G
by default accepts a regex, while -S
accepts a string, but it can be modified to accept regexes using the --pickaxe-regex
.
To illustrate the difference between -S<regex> --pickaxe-regex
and -G<regex>
, consider a commit with the following diff in the same file:
+ return !regexec(regexp, two->ptr, 1, ®match, 0);
...
- hit = !regexec(regexp, mf2.ptr, 1, ®match, 0);
While git log -G"regexec\(regexp"
will show this commit, git log -S"regexec\(regexp" --pickaxe-regex
will not (because the number of occurrences of that string did not change).
With Git 2.25.1 (Feb. 2020), the documentation is clarified around those regexes.
See commit 9299f84 (06 Feb 2020) by Martin Ågren (``).
(Merged by Junio C Hamano -- gitster
-- in commit 0d11410, 12 Feb 2020)
diff-options.txt
: avoid "regex" overload in the example \
Reported-by: Adam Dinwoodie
Signed-off-by: Martin Ågren
Reviewed-by: Taylor Blau
When we exemplify the difference between -G
and -S
(using --pickaxe-regex
), we do so using an example diff and git diff
invocation involving "regexec", "regexp", "regmatch", etc.
The example is correct, but we can make it easier to untangle by avoiding writing "regex.*" unless it's really needed to make our point.
Use some made-up, non-regexy words instead.
The git diff
documentation now includes:
To illustrate the difference between -S<regex> --pickaxe-regex
and -G<regex>
, consider a commit with the following diff in the same file:
+ return frotz(nitfol, two->ptr, 1, 0);
...
- hit = frotz(nitfol, mf2.ptr, 1, 0);
While git log -G"frotz\(nitfol"
will show this commit, git log -S"frotz\(nitfol" --pickaxe-regex
will not (because the number of occurrences of that string did not change).
Best Answer
Use the shell globbing syntax:
The syntax for
--exclude
is identical.Note that the star is escaped with a backslash to prevent it from being expanded by the shell (quoting it, such as
--include="*.cpp"
, would work just as well). Otherwise, if you had any files in the current working directory that matched the pattern, the command line would expand to something likegrep pattern -r --include=foo.cpp --include=bar.cpp rootdir
, which would only search files namedfoo.cpp
andbar.cpp
, which is quite likely not what you wanted.Update 2021-03-04
I've edited the original answer to remove the use of brace expansion, which is a feature provided by several shells such as Bash and zsh to simplify patterns like this; but note that brace expansion is not POSIX shell-compliant.
The original example was:
to search through all
.cpp
and.h
files rooted in the directoryrootdir
.