Git Tip of the Day – git grep

This will be a quick tip. Sometimes you need to search for something in your code, right? So you probably use:

$ grep -r 'foo()' /project

But unfortunately that may also print out results from files you didn’t want to search in, like backup files, compiled files (eg. build/ or dist/ directory, .pyc files for Python scripts,…), various temporary files, etc.

The solution is simple:

$ git grep 'foo()'

And voila! you searched only in the tracked files that are in your working tree. It ignores all files you don’t track (everything you have in .gitignore and anything you don’t track yet). What a beauty.

It behaves same as grep (performs recursion by default), supports same options and also adds a few git-specific ones.


Flattr this