What is git grep, and how does it differ from a normal file search?
Last updated
git grep searches the content of files tracked by Git, using Git's index and object database rather than scanning the filesystem. It's faster and more precise than a generic search because it knows exactly which files are versioned and can search any commit or tree, not just the working tree.
Searches tracked content only: Ignores untracked files, build artifacts, and anything in
.gitignore, so no noise fromnode_modulesor.git.Can search history, not just the disk:
git grep "foo" <commit>searches any commit or tree by reading objects directly.A tool like
grep -rcan only see the current working directory.
Fast and Git-aware: Uses the index and multiple threads, so it's typically quicker than recursive filesystem grep on a repo.
Handy options:
-nshow line numbers,-icase-insensitive,-llist only filenames,-efor patterns and--and/--orto combine them.
- https://git-scm.com/docs/git-grep/2.18.0
- https://git-scm.com/docs/git-grep
- https://www.kernel.org/pub/software/scm/git/docs/git-grep.html
- https://git-scm.com/book/cs/v2/Git-Tools-Searching
- https://git-scm.com/book/en/v2/Git-Tools-Searching
- https://git-scm.com/book/de/v2/Git-Tools-Suchen
- https://www.geeksforgeeks.org/git/git-grep/
- https://medium.com/@ahmedmuhumed/git-grep-command-c6682da0dbb3