git log to return only the commits made to the master branch?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In repositories with many feature branches, plain git log master can include commits that were authored elsewhere and later merged. If you want only commits made on the master branch path itself, you need the right history traversal options. The most useful flags are --first-parent, branch set subtraction, and path filtering.
What “Only Master Commits” Usually Means
Teams often mean one of two things:
- commits that are unique to
mastercompared with another branch - commits that occurred directly on the
masterfirst-parent line
These produce different outputs. Clarify which one you need before building reports or release notes.
Use --first-parent for Mainline History
For release history and merge overview, this is the most practical command:
--first-parent follows the mainline path of master and avoids expanding all commits from merged feature branches. You still see merge commits, but not every side-branch commit.
Add graph for readability:
Show Commits Unique to master
To show commits reachable from master but not from another branch:
Equivalent range form:
This is useful for release diff checks between long-lived branches.
Restrict to Non-Merge Commits
If you want only direct commit objects and no merge commits:
This is helpful when auditing individual hotfix commits applied directly on master.
Filter by Time, Author, or Path
Common practical filters:
Combining traversal control with targeted filters gives cleaner outputs than post-processing raw logs.
Detect Commits Introduced by Merge Operations
If release process uses merge commits, inspect merges and then inspect each merge if needed.
Then inspect one merge:
This helps tie release branch integration events to specific timelines.
Use git rev-list for Scriptable Output
When integrating with scripts, rev-list is stable and machine-friendly.
For unique commits relative to develop:
You can pipe hashes into other commands for CI checks or changelog tooling.
master Versus main
Many repositories now use main instead of master. Replace branch name accordingly:
If tooling is shared across repos, parameterize branch name rather than hardcoding.
Reliable Team Workflow
A practical team pattern:
- use
--first-parentfor release timeline - use range comparison for what changed since last release branch
- apply path or author filters only when needed
- pin command format in team docs for consistent reports
Consistency avoids confusion when different engineers generate different commit lists for the same question.
Common Pitfalls
- Using plain
git log masterand assuming it means direct master-only commits. - Confusing unique commit ranges with first-parent traversal history.
- Forgetting that merged branch commits are still reachable from master.
- Mixing branch names across repositories that use
mainandmasterdifferently. - Generating release notes without standardizing traversal flags.
Summary
- “Only master commits” is ambiguous, so define output intent first.
- '
--first-parentis best for mainline branch history.' - Branch range syntax shows commits unique to one branch versus another.
- '
--no-merges, time filters, and path filters refine output further.' - Standardize log commands in team workflows for reproducible history reports.

