I'd like to be able to use git grep to do a regular expression search through:
- the current commit
- a given commit
- a list of commits
- a range of commits
- a given commit all the way back to the parent commit
- all commits (and branches) in the entire repo
To do 1, you just do this:
git grep -n "some regex search string"
To do 2, you do this:
git grep -n "some regex search string" commit_hash_or_branch_name
To do 3, you just list all the commits like this:
git grep -n "some regex search string" commit1 commit2 commit3 commit4
But, how do we do 4, 5, or 6?
I tried this for 4, for instance, but it does not work:
git grep -n "some regex search string" beginning_commit~..end_commit
I thought this pattern might work since it works for git cherry-picks, as I explain here, but, it doesn't work for git grep.
See also
- Related, but not the same thing, since it looks for a string change, not a string existence: How can I search my ENTIRE git repo's commit history for a string change?
- https://git-scm.com/docs/gitrevisions (thanks, @JohnKugelman)
Update: see my new answer here instead: All about searching (via
grepor similar) in your git repositoriesHow to
git grepthrough whichever branches or commits you chooseAlright, with much effort, I figured it out. Here is how to do 4, 5, and 6:
4.
git grepa range of commitsTo search for
"search string"within a range of commits, fromcommit1, inclusive, tocommit2, inclusive, do this:Here is an example that searches for
"\bhey\b"(\bmeans "word boundary" in regular expressions) which you can run in my eRCaGuy_hello_world repo:Explanation
IFS=$'\n' read -r -d '' -a myarray <<< "$multiline_string"converts a multiline string into a bash array namedmyarray. See my answer here: How to read a multi-line string into a regular bash "indexed" array.To get a range of commits, you can use this trick:
Source where I got the necessary hints to learn the
git log --pretty=format:%Hpart: Get the short Git version hash.Example usage and output, when in my eRCaGuy_hello_world repo. Here you can see the first commit I specify at the very bottom of the output, and the last commit at the very top:
5.
git grepa given commit all the way back to the parent commitThis command is a subset of the command above, and is even simpler. To search for
"search string"fromcommit, inclusive, all the way back to the first (most-parent) commit, do this:Example from my eRCaGuy_hello_world repo:
6.
git grepa whole repository: ie: all commits (and branches) in the entire repoSame as just above, except use
--allin place of the branch name or commit hash:Example from my eRCaGuy_hello_world repo:
Alternative solutions and hints
A. To search for changes of a search pattern or word, you can do this:
See: How can I search my ENTIRE git repo's commit history for a string change?.
B. To search through just the changes introduced by each commit, use
git log -pand search interactively through the less viewer:You can also search for a match in your history like this:
git log -p, then press the / key, type your regular expression search string, and press Enter. Press n for "next match" or Shift + n for "previous match". The-pingit logshows the changes in "patch" format for each commit. And, since the output ofgit logis viewed in thelessviewer, you can use it to search the contents interactively.C. Use
git log --grep <regex> <branches>...as described here: Search a whole Git repository.