Sub topic 13
Reading history with merges: --first-parent and --no-merges
This chapter assumes you can run git log with --oneline and --graph, and that you know how a merge commit has two parents. It covers three options that control how merges appear in the log: --first-parent, --no-merges, and --merges.
Why merges clutter the log
A merge commit joins two lines of history. Its first parent is the branch you were on when you ran git merge. Its second parent is the tip of the branch you merged in.
By default, git log walks both parents. It shows every commit from the merged branch, interleaved with the commits on main by date. On a busy repository this makes main hard to read.
Following only the first parent
--first-parent tells git log to follow only the first parent of each merge. The merged branch is then one entry: the merge commit itself.
git log --first-parent --oneline mainOn a branch where you merge pull requests into main, the output is one line per merge, plus any commits made directly on main. This works as a release-style summary of what landed and when.
The output also shows what the option leaves out. Commits that were made on the feature branch no longer appear, so you lose the detail of how the work was done. Use it to get an overview, then look inside one merge when you need more.
Hiding or isolating merge commits
--no-merges does the opposite filtering. It hides commits that have more than one parent, but still walks both sides of every merge.
git log --no-merges --oneline -n 10This is useful when messages such as "Merge branch 'feature' into main" add nothing. Every real change is still listed.
--merges shows only merge commits.
git log --merges --oneline --since="1 month ago"The table compares the three options.
| Option | Merge commits shown | Commits from merged branches shown |
|---|---|---|
| (default) | Yes | Yes |
| `--first-parent` | Yes | No |
| `--no-merges` | No | Yes |
| `--merges` | Yes (only these) | No |
Looking inside one merge
To see what a single merge brought in, compare its first parent with the merge commit. Git names parents with ^1 and ^2.
git log --oneline M^1..M
git diff M^1 M --statM^1..M lists the commits that the merge added to the first-parent line. The diff shows the total change the merge made to the branch it was merged into. Replace M with a commit hash or a name such as HEAD.
You can also combine --first-parent with -p. The patch for a merge commit is then shown as a diff against its first parent.
git log --first-parent -p -n 3Exercise
You want a list of what was merged into main this month, one line per merged branch. You also want to read the actual change behind one of those merges, abc1234. Which commands do you use?
Show answer
Run git log --first-parent --merges --oneline --since="1 month ago" main for the list. Then run git diff abc1234^1 abc1234 to see the change the merge made, or git log --oneline abc1234^1..abc1234 to see the individual commits it brought in.