How to search through all Git and Mercurial commits in the repository for a certain string?
You can see dangling commits with git log -g
.
-g, --walk-reflogs
Instead of walking the commit ancestry chain, walk reflog entries from
the most recent one to older ones.
So you could do this to find a particular string in a commit message that is dangling:
git log -g --grep=search_for_this
Alternatively, if you want to search the changes for a particular string, you could use the pickaxe search option, "-S":
git log -g -Ssearch_for_this
# this also works but may be slower, it only shows text-added results
git grep search_for_this $(git log -g --pretty=format:%h)
Git 1.7.4 will add the -G option, allowing you to pass -G<regexp> to find when a line containing <regexp> was moved, which -S cannot do. -S will only tell you when the total number of lines containing the string changed (i.e. adding/removing the string).
Finally, you could use gitk to visualise the dangling commits with:
gitk --all $(git log -g --pretty=format:%h)
And then use its search features to look for the misplaced file. All these work assuming the missing commit has not "expired" and been garbage collected, which may happen if it is dangling for 30 days and you expire reflogs or run a command that expires them.
In Mercurial you use hg log --keyword
to search for keywords in the commit messages and hg log --user
to search for a particular user. See hg help log
for other ways to limit the log.
In addition to richq answer of using git log -g --grep=<regexp>
or git grep -e <regexp> $(git log -g --pretty=format:%h)
: take a look at the following blog posts by Junio C Hamano, current git maintainer
- Fun with "git log --grep"
- Fun with "git grep"
Summary
Both git grep and git log --grep are line oriented, in that they look for lines that match specified pattern.
You can use git log --grep=<foo> --grep=<bar>
(or git log --author=<foo> --grep=<bar>
that internally translates to two --grep
) to find commits that match either of patterns (implicit OR semantic).
Because of being line-oriented, the useful AND semantic is to use git log --all-match --grep=<foo> --grep=<bar>
to find commit that has both line matching first and line matching second somewhere.
With git grep
you can combine multiple patterns (all which must use the -e <regexp>
form) with --or
(which is the default), --and
, --not
, (
and )
. For grep --all-match
means that file must have lines that match each of alternatives.