How to Grep (Search Through) Committed Code in the Git History

Better Stack Team
Updated on July 25, 2024

To search through committed code in the Git history, you can use several methods. The most common and powerful tools for this purpose are git grep and git log combined with options like -G or -S.

Method 1: Using git grep

git grep is a command that searches for patterns in the repository files. By default, it searches the working directory, but you can also use it to search through committed code.

  1. Search in the Entire History: This command searches for the given pattern in all commits.

     
    git grep <pattern> $(git rev-list --all)
    

Method 2: Using git log -p

The git log command can be combined with -p to show the patch (diff) introduced in each commit, and you can use -G or -S to search for patterns.

  1. Search for a Regex Pattern in Commit Diffs:

     
    git log -p -G <pattern>
    

    This command searches for changes in the code that match the regex pattern.

  2. Search for a Specific String in Commit Diffs:

     
    git log -p -S <string>
    

    This command searches for commits that add or remove a specific string.

Method 3: Using git log with -grep

If you want to search through commit messages instead of the actual code, use the --grep option.

  1. Search Commit Messages for a Pattern:

     
    git log --grep=<pattern>
    

Method 4: Using git rev-list and git show

You can also combine git rev-list with git show to search through commits:

  1. Find Commits Containing a Specific String in Code:

     
    git rev-list --all | xargs git grep <pattern>
    

Example Usage

1. Using git grep:

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

This command searches for the string 'TODO' in all commits.

2. Using git log -p -G:

 
git log -p -G 'def function_name'

This command searches for commits where the pattern def function_name was introduced or removed.

3. Using git log -p -S:

 
git log -p -S 'initialize'

This command searches for commits that add or remove the exact string 'initialize'.

Combining git log with -oneline for a Concise Output

For a more concise output, especially when dealing with many commits, you can combine git log with --oneline.

  1. Search for a String and Show Commit Messages:

     
    git log --oneline -S 'initialize'
    

Summary

  • Use git grep <pattern> $(git rev-list --all) to search for a pattern in the entire commit history.
  • Use git log -p -G <pattern> to search for commits that introduce or remove a pattern.
  • Use git log -p -S <string> to search for commits that add or remove a specific string.
  • Use git log --grep=<pattern> to search commit messages for a pattern.

By leveraging these commands, you can effectively search through committed code in the Git history.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github