Sub topic 12
Bisecting through merges with --first-parent
This chapter assumes you can run a manual bisect session, you know the exit code contract (0 good, 1 to 127 except 125 bad, 125 skip), and you have a working reproducer script such as check.sh.
Why merges complicate a bisect
A plain bisect treats history as a graph and picks the midpoint among all commits reachable from the bad commit but not from the good one. On a repository where work happens on feature branches, that set includes every commit on every merged branch.
This causes two common problems:
- Commits in the middle of a feature branch may not build. The branch's author might have been halfway through a refactor, so the script exits 125 often.
- Commits on a feature branch may fail for a reason unrelated to the regression. Their parent chain may be missing a fix that already existed on
main.
If your goal is to find which merge brought the bug into main, the individual branch commits are noise.
What --first-parent does
The --first-parent option tells Git to follow only the first parent of each merge commit. On main, the first parent of a merge is the previous tip of main. The second parent is the tip of the merged branch. Following first parents gives you the sequence of states that main actually had.
Bisect now tests only A, M1, B, M2, and M3. The result is the first merge or main-line commit where the check fails. That is usually what you want: the merge that introduced the bug.
Running it
Pass the option to git bisect start, then run the script as before:
git bisect start --first-parent HEAD v1.4.0
git bisect run ./check.shHere HEAD is the bad commit and v1.4.0 is the good one. The option must be given at start, because it applies to the whole session.
Keep check.sh outside the working tree, or make sure it is not overwritten when Git checks out each commit. The script location rules from earlier in this tutorial still apply.
Reading the result
The first bad commit may now be a merge commit. Inspect it like this:
git show --stat <hash>
git log --oneline <hash>^1..<hash>^2The first command shows what the merge changed. The second lists the commits that the merged branch brought in, because <hash>^1 is the previous main line and <hash>^2 is the branch tip. The range ^1..^2 is a convenient way to list them, although it can include extra commits if the branch itself had merged main earlier.
Narrowing down further
Once you know the bad merge, you can look inside it. Start a second, ordinary bisect on the branch. Use the merge's first parent as good and its second parent as bad:
git bisect reset
git bisect start <hash>^2 <hash>^1
git bisect run ./check.shThe argument order is bad first and then good, so <hash>^2 is the bad endpoint and <hash>^1 is the good one. Validate both endpoints with your script before starting, as shown in the earlier chapter. If the branch was already failing before it merged, the second parent being bad is exactly what the check should confirm.
Trade-offs
| Approach | Tests | Finds | Risk |
|---|---|---|---|
| Default bisect | Every reachable commit | The exact commit | More untestable commits |
| `--first-parent` | Main-line commits only | The merge or main-line commit | Result is coarser |
Exercise
Your main branch is made of merge commits. A default bisect keeps hitting broken intermediate commits on feature branches. Which command starts a session that avoids them, and what might the first bad commit be?
Show answer
Run git bisect start --first-parent HEAD v1.4.0, then git bisect run ./check.sh. The first bad commit may be a merge commit. That merge is the one that brought the bug into main, and you can bisect inside it afterward.