Quick tip: Grep in Git

This quick tip is about two small features of Git I wish I had known about earlier, as it makes it way easier to search through it.

git-grep

git-grep is a way to search through your tracked files for whatever you provide. For example, if you want all files containing the word "index":

git grep index

Demo of

You can limit the search to specific files—for example, if you want to filter the above example to just JSON files:

git grep index -- '*.json'

Demo of  with filter

You can also search for multiple items in a single file—for example, if you want to find all files containing both "index" and "model":

git grep --all-match -e index -e model

Demo of  with multiple filters

git-log grep

git log has a grep function too, which is awesome for finding commit messages with a specific word or words. For example, if you want to find all commits about "Speaker" for DevConf, you could run:

git log --all --grep="Speaker"

 example