How to search in commit messages using command line?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Git provides git log --grep to search commit messages by keyword or regex pattern. This filters the commit history to only show commits whose messages match the given pattern. Combined with options like --author, --since, --all, and --format, you can narrow searches to specific contributors, time ranges, or branches. For searching code changes rather than messages, use git log -S (pickaxe) or git log -G (regex in diffs).
Basic Commit Message Search
Regex Patterns
Combining Search Filters
Formatting Output
Searching Across All Branches
Searching Code Changes (Not Messages)
Interactive Search with grep
Searching in a Date Range
Git Shortlog for Grouped Results
Common Pitfalls
- Forgetting that --grep is case-sensitive by default: Searching
--grep="Fix"will not match "fix" or "FIX". Add-ifor case-insensitive matching. This is especially easy to miss when searching for keywords that authors capitalize inconsistently. - Expecting --grep to search code changes:
--greponly searches commit messages. To find commits that changed specific code, use-S "string"(exact string in diff) or-G "regex"(regex in diff). These are different search modes that complement--grep. - Using multiple --grep without --all-match: By default, multiple
--greppatterns use OR logic (match any). If you want commits matching ALL patterns, add--all-match. This is the opposite of what most developers expect. - Not searching all branches:
git log --greponly searches the current branch by default. Commits on other branches or remote branches are invisible. Add--allto search the entire repository history across all branches and tags. - Confusing -S and -G:
-S "string"finds commits where the number of occurrences of the string changed (added or removed).-G "regex"finds commits where the diff matches the regex.-Sis for "when was this introduced/removed" while-Gis for "when was this line changed".
Summary
- Use
git log --grep="pattern"to search commit messages by keyword or regex - Add
-ifor case-insensitive search and--extended-regexpfor extended regex - Use
--all-matchto require all--greppatterns to match (default is OR) - Combine with
--author,--since,--until, and-- <path>to narrow results - Use
--allto search across all branches, not just the current one - Use
-Sor-Gto search code changes in diffs rather than commit messages
Related reading
- 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?
- How to set offset committed by the consumer group using Spark's Direct Stream for Kafka?
- How to set patience as default git diff algorithm
.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.