Git
Code Search
Programming
Software Development
Version Control

Git: search all history for a string

Codemia Knowledge Hub ·

To search all of Git history for a string, run git grep "string" $(git rev-list --all) to search every commit's snapshot, or git log -S"string" --source --all to find the commits that added or removed it. Use git log -G when you need a regular expression. These are useful for tracing when code was introduced or changed, debugging, and reviewing how a codebase evolved.

1. Search every snapshot with git grep

git grep searches the contents of any Git tree, not only the working directory.

Syntax:

bash
git grep <string> <revision>

Example: to search for "TODO" in all files across every commit in the repository:

bash
git grep "TODO" $(git rev-list --all)

git rev-list --all lists all commits, and git grep searches each snapshot for the string.

2. Find the commits that changed it with git log -S and -G

The -S and -G options of git log are known as the pickaxe search. Instead of searching snapshots, they show the commits where specific content was added or removed, which traces the evolution of that part of the code.

  • -S<string> finds commits that added or removed an instance of a string.
    Example:
bash
  git log -S"config()" --source --all

This lists commits that added or removed "config()".

  • -G<regex> works like -S but takes a regular expression, which is more flexible.
    Example:
bash
  git log -G"\bconfig\(\)" --source --all

This regex matches "config()" with word boundaries.

3. Search each branch with a loop

To search all branches, loop over them with git grep:

Script Example:

bash
for branch in $(git branch -a --format='%(refname:short)'); do
    git grep "search_term" $branch
done

This runs git grep for "search_term" on every branch.

Which command to use

CommandUse CaseDescription
git grep "search" $(git rev-list --all)Search for a string in all commits across all branchesUseful for locating when a string was added, modified, or removed
git log -S"string"Search commit changes for additions/removals of the specified stringGet the list of commits that show when a string appeared or disappeared
git log -G"regex"Regex search for content changes in commitsMore precise and flexible search to see the evolution of code segments
Scripted git grep across branchesSearches for a string in all branchesUseful in repositories with complex branch structures

Narrowing large searches

  • Refine the search: combine these tools and limit them by path, file type or date range.
  • Performance: searching the full history of a large repository can be slow and expensive, so narrow the range of commits or branches when you can.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions