How to search a Git repository by commit message?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you remember part of a commit message but not the commit hash, git log --grep is the right tool. It filters commit history by the text in the commit subject and body, which makes it useful for finding feature work, bug fixes, migration commits, or release markers.
The basic command is simple, but the useful part is knowing how to widen or narrow the search without scanning the entire log manually.
Start with git log --grep
The simplest form searches commit messages reachable from the current branch tip.
If Git finds matches, it prints the normal commit log output for those commits. That works well, but it is often easier to read in a compact format.
For everyday use, --oneline is often the best default because it shows the short hash and subject on one line.
Search Across All Branches
By default, git log only walks history reachable from the current HEAD. If the commit may live on another local branch, include --all.
This is important in repositories with long-lived release branches, merged feature branches, or partially abandoned work. Without --all, the commit may exist in the repository but still not show up.
Case-Insensitive and Regex Searches
Commit messages are just text, so Git lets you search them with regular-expression semantics.
You can also combine multiple --grep patterns. By default, multiple patterns act like OR.
If you want all patterns to match, add --all-match.
That is useful when a single keyword is too broad.
Narrow the Result Set Further
You can combine message search with author, date, or path filters.
If you care only about commits that touched a specific file or directory, add a pathspec after --.
That command says: search commit messages for timeout, but only among commits that touched src/api/.
Format the Output for Faster Triage
When you are searching rather than reading history in detail, custom formatting helps.
This prints:
- short hash
- date
- author
- subject
That is usually enough to identify the commit you want before running git show <hash>.
A Practical Workflow
A fast workflow looks like this:
- search with
git log --all --grep="keyword" --oneline - tighten with
--author,--since, or another--grep - inspect the winning commit with
git show <hash>
That sequence is faster than scanning a graphical history viewer for most targeted searches.
Common Pitfalls
- Forgetting
--alland assuming the commit does not exist when it is simply unreachable from the current branch. - Searching only for the exact full sentence when a smaller keyword would match more reliably.
- Expecting
--grepto search file contents. It searches commit messages, not diffs or source text. - Forgetting
--all-matchwhen multiple--greparguments should all be required. - Using the default verbose log format when
--onelineor a custom--formatwould make the search result much easier to scan.
Summary
- Use
git log --grep="pattern"to search commit messages. - Add
--allwhen the commit may live outside the current branch history. - Use
--regexp-ignore-case, multiple--grepoptions, and--all-matchto refine the search. - Combine message search with author, date, and path filters when needed.
- Pair the search with
git showonce you have the commit hash you want.
Related reading
- How to search a Git repository by commit message?
- How to search in commit messages using command line?
- How to search through all Git and Mercurial commits in the repository for a certain string?
- How to see an HTML page on Github as a normal rendered HTML page to see preview in browser, without downloading?
- How to see changes to a file before commit?
- How to see commits that were merged in to a merge commit?
- How to see the changes between two commits without commits in-between?
- How to see the changes between two commits without commits in-between?
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.