Sub topic 2
A manual bisect session: start, good, bad, reset
This chapter walks through a complete git bisect session by hand. You will mark the two endpoints, test the commits Git picks for you, read the result, and end the session cleanly. Later chapters replace the manual testing with a script. The session mechanics stay the same, so it pays to know them first.
This chapter assumes you can make and switch commits, branches and tags in Git. It also assumes you can build and run your project's tests from the command line.
What a session is
A bisect session is a temporary state in your repository. During the session, Git does four things:
- It records which commit you called good and which you called bad.
- It checks out a commit halfway between them, in detached HEAD state.
- It waits for you to say whether that commit is good or bad.
- It narrows the range and repeats until one commit remains.
Each answer cuts the remaining range roughly in half. A range of 1,000 commits needs about 10 tests, because log2(1000) is just under 10. A range of 100,000 commits needs about 17.
A few terms recur in this chapter:
- Good commit: a commit where the bug is absent.
- Bad commit: a commit where the bug is present.
- First bad commit: the earliest commit where the bug appears. This is what bisect finds.
- Detached HEAD: a state where HEAD points at a commit instead of a branch name. Bisect checks out commits this way.
The loop at a glance
Before you start
Pick the two endpoints.
- The bad endpoint is usually
HEADor the tip of the branch where you saw the bug. - The good endpoint is any commit where you are sure the bug did not exist. A release tag such as
v1.4.0works well. If you do not know a good commit, check out older ones until you find one.
The good commit must be an ancestor of the bad commit. If it is not, Git has no linear range to search and will complain.
Check your working tree before you begin:
git statusBisect checks out many commits. Uncommitted changes can block a checkout, or they can follow you from commit to commit and confuse your tests. Commit or stash them first:
git stash push -u -m "before bisect"Starting the session
Here is the shortest way to begin. The first argument is the bad commit and the second is the good one:
git bisect start HEAD v1.4.0You can also do it in separate steps, which is clearer when you are learning:
git bisect start
git bisect bad HEAD
git bisect good v1.4.0git bisect bad and git bisect good with no argument mark the commit you currently have checked out. With a commit argument they mark that commit. After the second mark, Git has both endpoints and immediately checks out a midpoint:
Bisecting: 31 revisions left to test after this (roughly 5 steps)
[3f9c2ab71d0e5c8a4b1d6e7f8a9b0c1d2e3f4a5b] Refactor config loaderThe numbers depend on your history, so yours will differ. The first line estimates the work: about 31 candidates remain after this commit, and roughly 5 more tests will finish the job. The second line shows the commit now checked out.
Testing a commit and reporting
At each step, build the project and check for the bug the way you normally would. Suppose the bug is that ./run_regression_test.sh fails. Run it on the checked-out commit:
make && ./run_regression_test.shThen tell Git what you saw. If the test passes, the bug is absent:
git bisect goodIf the test fails, the bug is present:
git bisect badGit moves to a new midpoint and prints a new estimate:
Bisecting: 15 revisions left to test after this (roughly 4 steps)
[8d1e4c0b92a7f6e5d4c3b2a1908f7e6d5c4b3a29] Add retry to upload clientRepeat the test and report until Git stops asking.
Reporting the wrong answer
A mistaken good or bad is the most common manual error. It sends the search into the wrong half of the range, and the final answer will be wrong with no warning. Slow builds, stale build artifacts and tired humans all cause this.
If you notice a wrong mark, look at what you have recorded:
git bisect logThe log lists every mark in order. You can save it to a file, edit out the bad line, and replay it. Saving and replaying sessions is covered later in this tutorial.
Commits you cannot test
Sometimes the midpoint commit does not build, for a reason unrelated to your bug. Do not guess good or bad for it. Tell Git to pick a different nearby commit:
git bisect skipSkipping is only a fallback. If many commits in the middle of the range are untestable, Git may end with a set of suspects instead of one commit. A later chapter shows how a script can skip automatically.
Reading the result
When only one candidate remains, Git stops and prints a report:
a7b3c9d1e2f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8 is the first bad commit
commit a7b3c9d1e2f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8
Author: Dana Lee <[email protected]>
Date: Tue Oct 14 11:02:37 2026 +0000
Switch upload client to streaming mode
src/upload/client.c | 42 +++++++++++++++++++++++++++++-----------
1 file changed, 31 insertions(+), 11 deletions(-)Git is saying that this commit is the earliest one where the bug appears. Its parent was marked good and it was marked bad. You are now still in the session, with this commit checked out.
Inspect the commit before you act on it:
git show a7b3c9dRead the diff and ask whether it plausibly causes your symptom. Bisect only reports where your test flipped from pass to fail. If the test was wrong, the answer is wrong too.
Inspecting progress mid-session
Two commands help while the session is running:
git bisect logprints every mark made so far.git bisect visualizeopens the remaining candidate commits ingitk. If no graphical viewer is available, usegit bisect visualize --onelineto print them in the terminal.
Ending the session with reset
A session does not end when Git prints the result. It ends when you run:
git bisect resetThis clears the bisect state and returns you to the branch you were on when you ran git bisect start. Bisect records that starting point in .git/BISECT_START. If you had stashed changes earlier, bring them back now:
git stash popIf you want to land somewhere other than your starting point, pass a commit or branch:
git bisect reset mainForgetting to reset is a classic mistake. You stay in detached HEAD at the culprit commit. New commits made there belong to no branch, and git status shows a bisect session in progress on newer Git versions.
Where Git keeps the state
You do not need these details for daily use, but they explain a few behaviors:
| Location | Contents |
|---|---|
| `.git/BISECT_START` | The branch or commit you started from, used by `reset` |
| `.git/BISECT_LOG` | The record of every command, shown by `git bisect log` |
| `refs/bisect/bad` | The current bad endpoint |
| `refs/bisect/good-*` | One ref per good commit you marked |
Because the state lives inside .git, it survives closing your terminal. You can leave a session and come back to it later. It is also why a half-finished session keeps affecting your repository until you reset it.
A complete session in one view
This sequence shows the whole workflow, from a clean tree to a clean tree:
git stash push -u -m "before bisect"
git bisect start HEAD v1.4.0
# Repeat until Git reports the first bad commit:
make && ./run_regression_test.sh
git bisect good # or: git bisect bad
git show HEAD # inspect the reported commit
git bisect reset
git stash popCount how many times you typed the test command and the mark. That repetition is what git bisect run removes. The next chapters define the exit-code contract that lets a script supply the good or bad verdict for you.
Common problems
- Git says the good commit is not an ancestor. Check that you picked the right tag and that it sits in the history of your bad commit.
git merge-base --is-ancestor v1.4.0 HEADreturns exit status 0 when it does. - Checkout fails because of local changes. Stash or commit them, then retry.
- The result looks unrelated to the bug. Suspect a wrong mark, a stale build, or a test that fails for a second reason. Review
git bisect logand rerun the test by hand on the reported commit and on its parent. - You started with the endpoints reversed. Git notices the mismatch only when the evidence conflicts. Run
git bisect resetand start again.
Exercise
A range between a good tag and HEAD contains 500 commits. About how many tests will a manual bisect need, and what command should you run after Git reports the first bad commit?
Show answer
About 9 tests, since log2(500) is roughly 8.97 and Git rounds up in practice. After the report, run git bisect reset to leave the session and return to your original branch.
Exercise
You are in the middle of a session and realize you ran git bisect good on a commit that actually had the bug. What do you do first to understand and repair the state?
Show answer
Run git bisect log to see every mark in order. Save it with git bisect log > bisect.log, run git bisect reset, and edit the wrong line out of the file. Then run git bisect replay bisect.log to restore the corrected session and continue testing.