Sub topic 12
Bisecting through merges with --first-parent
This chapter assumes you can run git bisect run with a script that follows the exit code contract (0 good, 1 to 127 except 125 bad, 125 skip). It also assumes you know what a merge commit is and what "first parent" means. Everything here applies to a repository whose main branch is built from merge commits, such as one that uses pull request merges.
Why plain bisect can mislead on merge-heavy history
A merge commit has two or more parents. By convention, the first parent is the branch you were on when you ran git merge, which is usually main. The other parents are the tips of the branches that were merged in.
By default, git bisect treats every commit reachable from the bad commit and not reachable from the good commit as a candidate. That includes every commit on every merged feature branch. This has three costs:
- Feature-branch commits are often intermediate work. They may not build, may fail unrelated tests, or may predate a fix that landed on
main. Each one needs a skip (125) or gives a misleading verdict. - A feature-branch commit is tested without the rest of
main. A bug that appears only when the two lines of work are combined can't be pinned on one side branch commit. Bisect may land on a commit that is only "bad" because of a stale base. - The answer is often not what a reviewer wants. If your team ships through pull requests, the useful question is "which merge introduced this?" rather than "which of the 14 commits inside that pull request?"
--first-parent restricts the search to the chain of first parents from the bad commit back toward the good commit.
Each commit on the main line is an integrated state: a merge commit contains the result of combining the feature branch with main at that moment. That is the state your CI and your users saw.
Running a first-parent bisect
Pass --first-parent to git bisect start. The option needs Git 2.29 or later, so check git --version first.
git bisect start --first-parent HEAD v1.4.0
git bisect run ./check.shChoose the good commit from the first-parent chain, such as a release tag on main. If the good commit sits on a side branch, the first-parent walk may not pass through it, and the search range is harder to reason about.
You can predict the number of steps before you start by counting the first-parent commits in the range:
git rev-list --first-parent --count v1.4.0..HEAD
git rev-list --count v1.4.0..HEADThe first number is the N that bisect searches. Bisect needs about log2(N) tests, rounded up. Suppose the range holds 400 first-parent commits and 1,500 commits in total. Then 9 tests suffice with --first-parent (2^9 = 512) instead of 11 (2^11 = 2048).
The saving in step count is small because the cost grows with the logarithm. The larger benefit is quality. Every commit tested was a state that someone merged on purpose, so far fewer need exit code 125, and a verdict of "bad" is more trustworthy.
What the output looks like
When bisect finishes, it names the first bad commit. On a first-parent search that is usually a merge commit:
3f9c1d2e8a7b6c5d4e3f2a1b0c9d8e7f6a5b4c3d is the first bad commit
commit 3f9c1d2e8a7b6c5d4e3f2a1b0c9d8e7f6a5b4c3d
Merge: 81ab2c3 d4e5f60
Author: Dana Ortiz <[email protected]>
Date: Tue Nov 17 10:42:08 2026 +0000
Merge pull request #482 from dana/export-streamingThe Merge: line lists the abbreviated parents. The first is the previous state of main, and the second is the tip of the merged branch. The result can also be an ordinary commit, if someone committed directly to main, or a fast-forwarded or squash-merged change. Those are part of the first-parent chain too.
Narrowing down inside the merge
--first-parent tells you which merge introduced the bug. It does not tell you which commit inside the merged branch is responsible. The second stage depends on what you find.
First, record the merge hash before you reset, because git bisect reset removes the bisect refs:
MERGE=$(git rev-parse refs/bisect/bad)
git bisect resetThen check the two parents with your script:
git checkout "$MERGE^1" && ./check.sh; echo "first parent exit: $?"
git checkout "$MERGE^2" && ./check.sh; echo "second parent exit: $?"The first parent should be good, since the merge is the first bad commit on the main line. The second parent decides the next step:
| Result for the second parent | Meaning | Next step |
|---|---|---|
| Bad (non-zero exit) | The feature branch itself contains the bug | Bisect the side branch (below) |
| Good (exit 0) | Neither side is bad alone. The bug comes from combining them, or from the conflict resolution | Inspect the merge itself |
When the branch is at fault, run a second, ordinary bisect limited to the commits the merge brought in. The range MERGE^1..MERGE^2 contains exactly those commits.
git bisect start "$MERGE^2" "$MERGE^1"
git bisect run ./check.sh
git bisect resetExpect more skips here. Commits inside a feature branch are less polished, so a build failure should return 125 from the script, as in the earlier chapters. Also note that these commits sit on the branch's older base. If the branch was cut long before the merge, the old base may lack fixes that main has, and a failure may come from that rather than from the branch's own change.
When the merge itself is the problem
If MERGE^2 is good, look at what the merge did that neither parent did:
git show --cc "$MERGE"
git show --remerge-diff "$MERGE"
git diff "$MERGE^1" "$MERGE"git show --ccshows a combined diff, which for a clean merge is usually empty or small. Large output means the merge contains changes that are in neither parent.--remerge-diff(Git 2.36 or later) re-creates the automatic merge and shows how the committed result differs from it. This exposes a manual conflict resolution that went wrong.git diff "$MERGE^1" "$MERGE"shows everything the merge brought intomain. Read it with the failing behavior in mind.
A logical conflict is a typical cause. Branch A renames a function, branch B adds a new call to the old name, and Git merges both without a textual conflict. Each side passes its tests alone. Only the merge fails.
Checking the first-parent result
A short checklist keeps you from trusting a result that it doesn't support:
- Confirm the endpoints with your script as before. The good endpoint must be on the first-parent chain, and the bad endpoint is usually
HEADofmain. - After the search, run the script on the reported commit and on its first parent. They should give bad and good.
- Check that the reported commit changes code related to the failure, for example with
git show --stat.
git checkout "$MERGE" && ./check.sh; echo "result commit exit: $?"
git checkout "$MERGE^1" && ./check.sh; echo "its first parent exit: $?"The expected output is a non-zero code for the first command and 0 for the second.
Gotchas
- Squash and rebase workflows. If every pull request lands as a single squashed or rebased commit,
mainis already linear and--first-parentchanges nothing. It is harmless in that case. - Merges in the other direction. If developers routinely merge
maininto their feature branches, those merges havemainas a side parent and are never onmain's first-parent chain. The chain stays clean. The reverse is a problem: if someone merged a long-lived branch the wrong way around (mainmerged into a release branch, then the release branch fast-forwarded), the first-parent chain may not be the history you expect. Check it withgit log --first-parent --oneline. - Octopus merges. A merge with three or more parents is still one step on the first-parent chain, and
MERGE^3and later parents are the extra branches. Test each second-stage parent separately. - Scripts that read history. If your check script uses
git logorgit describeto compute a version, it sees the real history, not just the first-parent view. Keep the script independent of history where you can. - Flaky failures. First-parent narrows the commits, but a flaky test still gives wrong verdicts at any step. Handle flakiness inside the script, for example by repeating the test, as covered in the flaky failures chapter.
You can also save a first-parent session with git bisect log > bisect.log and replay it with git bisect replay bisect.log. The log includes the --first-parent start option, so the replay uses the same restricted search.
Exercise
A repository has 1,000 commits between the good tag and HEAD, of which 120 are on the first-parent chain. Roughly how many tests does a first-parent bisect need, and what two things should you do after it names a merge commit M to find the cause inside the merged branch?
Show answer
About 7 tests, because log2(120) is just under 7 (2^7 = 128). After the result, save the hash with MERGE=$(git rev-parse refs/bisect/bad) and run git bisect reset. Then run your script on M^2 to see whether the merged branch is bad on its own. If it is, run git bisect start "$MERGE^2" "$MERGE^1" followed by git bisect run ./check.sh to search only the commits that the merge brought in. If M^2 is good, the bug comes from combining the branches, so inspect the merge with git show --cc or --remerge-diff.
Exercise
You run git bisect start --first-parent HEAD v1.4.0 and bisect run reports a merge commit as the first bad commit. You then test the merge's second parent with your script and it exits 0. What does that tell you, and why would a normal bisect of the side branch find nothing?
Show answer
It tells you that the feature branch is good on its own and that the first parent is good too, so the failure comes from the combination of the two, such as a logical conflict or a mistake in the conflict resolution. A bisect of MERGE^1..MERGE^2 would have a good endpoint on both ends of the range, so it has no bad commit to find. Inspect the merge with git show --cc "$MERGE" or git show --remerge-diff "$MERGE" instead.