Within a specific commit range (for example commits belonging to the current branch I’m working on), is it possible to find all the commits, filenames and line numbers where a pattern was added/modified?
The goal is to be able to visit those places in my editor (for example vim quickfix).
Background and details
git grep
can be used to find the location of a specific pattern in any file in the repo, but it looks at the current state and finds any appearance, no matter how long ago that line was touched. On top of git grep
one can use git-jump
(e.g. git jump grep banana
) to open vim with a populated quickfix list of all the locations (but again, no matter how old).
I would like to somehow limit the scope in time (number of commits) of the grep. What are my options? I have considered:
git grep
- does not seem to allow for a commit range – which makes sense. So probably not usable?
git log -G banana --patch HEAD~10^ | grep banana
- shows the lines including banana, but how can I get file and line number information?
- Command lists each commit matching the
log -G
commit once (e.g.--pretty=oneline
gives me just the hash and commit message), but should get one output per matching line with file and line number information. Is it possible to get several “hits” per commit withgit log
? - The files and line numbers are not necessarily preserved from commit to commit but the location info is usually close enough to still be valuable.
- Similar question and discussion at How can I grep Git commits for a certain word?
git blame
- Seems like I would need to build a complex command to get the information I want since it needs to look through all files in the repo?
- Editor specific tricks?
- Vim has a git plugin called fugitive that can traverse commit history (
:Gclog
) or git-grep matches (:Ggrep
), but is there some trick to do this grepping and traversing the commit log at the same time?
- Vim has a git plugin called fugitive that can traverse commit history (
See also similar but not identical question:
How can I grep Git commits for a certain word?
4