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:
Example: to search for "TODO" in all files across every commit in the repository:
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:
This lists commits that added or removed "config()".
-G<regex>works like-Sbut takes a regular expression, which is more flexible.Example:
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:
This runs git grep for "search_term" on every branch.
Which command to use
| Command | Use Case | Description |
git grep "search" $(git rev-list --all) | Search for a string in all commits across all branches | Useful for locating when a string was added, modified, or removed |
git log -S"string" | Search commit changes for additions/removals of the specified string | Get the list of commits that show when a string appeared or disappeared |
git log -G"regex" | Regex search for content changes in commits | More precise and flexible search to see the evolution of code segments |
Scripted git grep across branches | Searches for a string in all branches | Useful 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.
Related reading
- Search code inside a Github project
- Secure access to a private helm repository
- See changes to a specific file using git
- See diff between current state and last commit
- See what's in a stash without applying it
- Selectively revert or checkout changes to a file in Git?
- Set up git to pull and push all branches
- Set Visual Studio Code to be global Git editor on OSX
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.