Grepping in Git: How to Search Git Repository Revisions, Working Trees, Commit Messages, etc.

  • To search content of all tracked files in the current working tree for a pattern:

    git grep
  • To search content of all commit messages for a pattern (‘-E’ for extended grep):

    git log [-E] --grep
  • To search content of all commit diffs for lines that add or remove a pattern (‘-w’ for pattern only at word boundary):

    git [-w] log -G
  • To search content of entire working trees of previous revisions for a pattern:

    git grep  $(git rev-list --all)

Note that Git supports POSIX Basic Regular Expression. Which is pretty, well, basic. Not much more beyond matching beginning/ending of lines (’^’ and ‘$’), the “match anything” dot (’.’), zero or more of preceding start (’*’), and some quantifiers. Took me a long time to figure out why a lot of my searches using some of the “fancier” operators (from a pre-1970 perspective) were not working.

Share