Sub topic 9
Running a bisect by hand
This chapter assumes you know how to make commits and switch branches, and that you have read the previous chapter on why bisect works as a binary search. Here you will run a bisect step by step, using the manual commands.
What you need before you start
A bisect needs two facts about your history:
- One commit where the bug is present (the bad commit). This is usually your current commit.
- One older commit where the bug is absent (the good commit). A release tag is a common choice.
You also need a way to test a commit and decide whether it is good or bad. By hand, that means running the program or a test yourself at each step. Commit the changes you are working on, or stash them first, because bisect checks out other commits.
The basic loop
git bisect start
git bisect bad
git bisect good v1.2.0
# Git checks out a middle commit. Test it, then report the result:
git bisect good # or: git bisect bad
# repeat until Git prints the first bad commit
git bisect resetEach command does one job:
git bisect startbegins a session. Git records where you were so it can return you there later.git bisect badmarks the current commit (HEAD) as bad. You can also name a commit:git bisect bad <commit>.git bisect good v1.2.0marks the tag as good. Git now knows the range and checks out a commit near the middle.- After you test that commit, you run
git bisect goodorgit bisect bad. Git throws away half of the remaining range and checks out the next midpoint. - When only one candidate is left, Git prints a message like
<hash> is the first bad commit, with the commit's author, date, and message. git bisect resetends the session and returns you to the branch you started on.
The checkouts leave you in a detached HEAD state. That is expected. Do not commit in this state, because the commits would be lost when you reset.
A worked session
Suppose a login page crashes on main, and it worked at tag v1.2.0. About 100 commits separate them.
git bisect start
git bisect bad
git bisect good v1.2.0Git replies with the number of steps it expects, roughly seven for 100 commits, and checks out a middle commit. You run the app and the login page works, so you report:
git bisect goodGit moves to a commit in the newer half. This one crashes:
git bisect badYou keep answering until Git names the first bad commit. You can then inspect it with git show <hash>, and finish with git bisect reset.
The diagram shows how one session moves through its states.
Useful companion commands
git bisect logprints the decisions you have made so far.git bisect visualize(orgit bisect view) opens the remaining range in a viewer such asgitk, or prints it withgit logif no graphical viewer is available.git bisect start <bad> <good>is a shorter form that sets both endpoints at once, for examplegit bisect start HEAD v1.2.0.
Common errors
- Answering for the wrong commit. Always test the commit Git just checked out, not the one you were on before. Run
git statusorgit log -1if you lose track. - A flaky test. One wrong answer sends the search into the wrong half of the history, and Git will name an innocent commit. If a result looks suspicious, review
git bisect logand restart. - A wrong "good" commit. If the commit you called good actually has the bug, the result is meaningless. Check that commit before you start.
- Forgetting
git bisect reset. Until you run it, you stay in the middle of history. Runninggit statusshowsHEAD detachedand mentions the bisect. - Uncommitted changes. If checkout would overwrite them, Git refuses to move. Stash them first, then pop them after the reset.
Exercise
Your current commit has a bug and tag v2.0 does not. Write the commands that start a bisect, report the first midpoint as bad, and then end the session after Git names the culprit.
Show answer
git bisect start HEAD v2.0 (or: git bisect start, git bisect bad, git bisect good v2.0), then test the commit Git checks out and run git bisect bad, continue answering good or bad until Git prints the first bad commit, then run git bisect reset.
Exercise
Midway through a bisect, you cannot remember which answers you have given. Which command shows them, and why should you not commit a fix while the session is active?
Show answer
git bisect log lists your decisions. Bisect leaves you on a detached HEAD, so any commit made there is not on a branch and is left behind when git bisect reset returns you to your original branch.