Git Bisect

Updated · 11 min read
mediumGit 2.53gitgit-bisectdebuggingbinary-searchinterview

git bisect is a command that finds the commit that introduced a bug, using a binary search through history. You tell Git one commit where the bug exists (bad) and one where it didn't (good). Git checks out the commit halfway between them, you test it and say "good" or "bad", and Git halves the range again, until only one commit is left: the first bad one. It is used when you know that something broke, but not when or why.

It is the same game as guessing a number between 1 and 100 with "higher" or "lower" answers: you never need more than 7 guesses. Between 1,000 commits, bisect needs about 10 tests.

Each test halves the commits still in doubt

Syntax

Terminal
git bisect start [<bad> [<good>]]
git bisect good | bad | skip [<commit>]
git bisect run <command>
git bisect reset
CommandDescription
git bisect startIt is used to begin a search. Optionally give the bad and good commits at once.
git bisect bad [<commit>]It is used to mark the current (or given) commit as having the bug.
git bisect good [<commit>]It is used to mark the current (or given) commit as free of the bug.
git bisect skipIt is used to skip a commit that can't be tested, for example because it doesn't build.
git bisect run <command>It is used to let a command test each step: exit code 0 means good, 1 to 127 (except 125) means bad, 125 means skip.
git bisect logIt is used to show the answers given so far.
git bisect resetIt is used to end the search and return to where you started.

The Starting Point

The Fee Tracker has ten commits since release v1.0.0. Somewhere among them, the term fee changed from 12500 to 1250, and nobody noticed until a parent complained:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Term fee: 12500" > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
git tag v1.0.0
for i in 1 2 3 4 5 6 7 8 9 10; do
  echo "Note $i" >> notes.txt
  if [ "$i" = 7 ]; then
    sed -i.bak "s/12500/1250/" fees.txt
    rm fees.txt.bak
  fi
  git add .
  git commit --quiet -m "Update notes ($i)"
done
git log --oneline
Output
afb95fa (HEAD -> main) Update notes (10)
3b15025 Update notes (9)
c0fa38e Update notes (8)
5a4c434 Update notes (7)
a0f0b4d Update notes (6)
949b043 Update notes (5)
fcc1516 Update notes (4)
dc7951b Update notes (3)
51eb105 Update notes (2)
e956e96 Update notes (1)
b077810 (tag: v1.0.0) Add the term fee

The loop makes ten commits that each add a line to notes.txt. Commit 7 also changes the fee, with a message that doesn't mention it, as real bugs often hide. Reading every diff would work for ten commits, but not for a thousand.

Example 1: Bisect by Hand

The test is simple: does fees.txt still say 12500?

Terminal
grep "Term fee" fees.txt
git bisect start
git bisect bad
git bisect good v1.0.0
Output
Term fee: 1250
status: waiting for both good and bad commits
status: waiting for good commit(s), bad commit known
Bisecting: 4 revisions left to test after this (roughly 2 steps)
[949b043103b499dccc03276f1a3614f10b3e8414] Update notes (5)

Explanation: the current commit is bad (1250), and v1.0.0 is good. There are 10 commits in between, so Git checked out one near the middle, "Update notes (5)", and estimates roughly 2 more steps after this one. Navika tests it:

Terminal
grep "Term fee" fees.txt
git bisect good
Output
Term fee: 12500
Bisecting: 2 revisions left to test after this (roughly 1 step)
[5a4c434b3da087703a7a7058af758c4710ce811a] Update notes (7)

Commit 5 is good, so the bug came later. Git moves to commit 7:

Terminal
grep "Term fee" fees.txt
git bisect bad
Output
Term fee: 1250
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[a0f0b4d615cade17b165896a99a93511647c194e] Update notes (6)

Commit 7 is bad. The bug is in commit 6 or 7, so Git tests commit 6:

Terminal
grep "Term fee" fees.txt
git bisect good
Output
Term fee: 12500
5a4c434b3da087703a7a7058af758c4710ce811a is the first bad commit
commit 5a4c434b3da087703a7a7058af758c4710ce811a
Author: Navika Anamaneni <navika@example.com>
Date:   Mon Jan 5 10:44:00 2026 -0600
 
    Update notes (7)
 
 fees.txt  | 2 +-
 notes.txt | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

Explanation: after three answers, Git names the culprit, Update notes (7), and shows its details and the files it changed: fees.txt is among them, which a note commit shouldn't touch. git show on it shows the exact line. Now Navika ends the search, which returns her to the branch she started on:

Terminal
git bisect reset
Output
Previous HEAD position was a0f0b4d Update notes (6)
Switched to branch 'main'

Example 2: Let a Command Test Each Step

Testing by hand is slow and error-prone. If a command can tell good from bad by its exit code, git bisect run does the whole search alone. Navika writes the check as a small script, outside the repository's tracked files, so that it exists in every commit Git checks out:

Terminal
cat > ../check-fee.sh <<'EOF'
#!/bin/sh
grep -q "Term fee: 12500" fees.txt
EOF
chmod +x ../check-fee.sh
git bisect start HEAD v1.0.0
git bisect run ../check-fee.sh
Output
Bisecting: 4 revisions left to test after this (roughly 2 steps)
[949b043103b499dccc03276f1a3614f10b3e8414] Update notes (5)
running '../check-fee.sh'
Bisecting: 2 revisions left to test after this (roughly 1 step)
[5a4c434b3da087703a7a7058af758c4710ce811a] Update notes (7)
running '../check-fee.sh'
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[a0f0b4d615cade17b165896a99a93511647c194e] Update notes (6)
running '../check-fee.sh'
5a4c434b3da087703a7a7058af758c4710ce811a is the first bad commit
commit 5a4c434b3da087703a7a7058af758c4710ce811a
Author: Navika Anamaneni <navika@example.com>
Date:   Mon Jan 5 10:44:00 2026 -0600
 
    Update notes (7)
 
 fees.txt  | 2 +-
 notes.txt | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
bisect found first bad commit

Explanation: git bisect start HEAD v1.0.0 gave the bad and good commits in one step. git bisect run then ran the script at each step: grep -q exits with 0 when it finds the text (good) and 1 when it doesn't (bad). Git found the same commit, with no questions asked, and printed bisect found first bad commit.

Terminal
git bisect reset
Output
Previous HEAD position was a0f0b4d Update notes (6)
Switched to branch 'main'

In a Java project, the test is usually the build: git bisect run mvn -q test, or a single JUnit test class with mvn -q -Dtest=FeeCalculatorTest test. Maven exits with 0 when the tests pass and 1 when they fail, exactly what bisect needs. A test that didn't exist in older commits can be kept in a file outside the repository and copied in by a small script.

Other Useful Commands

CommandDescription
git bisect logIt is used to show every answer so far. Save it with > bisect.log to repeat the search later with git bisect replay bisect.log.
git bisect visualize (or view)It is used to show the remaining suspects in git log or gitk.
git bisect start --term-old=slow --term-new=fastIt is used to search for any change, not just bugs, with your own words for good and bad.
git bisect skip <range>It is used to skip several commits at once.

Common Mistakes

Swapping good and bad. Marking a good commit as bad sends the search in the wrong direction. If an answer was wrong, check git bisect log, and start again.

Forgetting git bisect reset. You stay on a detached HEAD in the middle of history. git status reminds you that you are bisecting.

A test script inside the repository that changes between commits. Keep it outside, or untracked, so every step runs the same test.

Testing commits that don't build. Use git bisect skip, or exit code 125, instead of calling them bad.

Uncommitted changes when starting. Bisect checks out other commits. Commit or stash first.

Under the Hood

Bisect keeps its state in references under refs/bisect/ (refs/bisect/bad, refs/bisect/good-<id>) and in .git/BISECT_LOG. At each step, Git computes the set of commits that are ancestors of the bad commit but not of any good commit, and picks the one that splits that set most evenly, then checks it out in detached HEAD. With n suspects, each answer roughly halves n, so the search takes about log2(n) steps: 4 for 10 commits, 10 for 1,000, 20 for a million. With merges in the history, Git still works on the set of suspects, not on a straight line, so it handles branchy history correctly.

Interview Corner

What is git bisect?

A command that finds the commit that introduced a change, typically a bug, by binary search. You mark a known bad and a known good commit, and Git repeatedly checks out a midpoint for you to test until only the first bad commit is left.

How do you automate git bisect?

git bisect start <bad> <good>, then git bisect run <command>. The command's exit code decides each step: 0 is good, 1 to 127 (except 125) is bad, and 125 skips the commit.

How many steps does bisect need?

About log2(n) for n candidate commits: around 10 steps for 1,000 commits.

What do you do with a commit that can't be tested during bisect?

git bisect skip, or exit with 125 from the script in git bisect run. Git then tests a nearby commit instead.

How do you finish a bisect session?

git bisect reset, which returns HEAD to the branch and commit you were on before git bisect start.

Practice

Challenge: when did the exam fee appear? Add an exam fee line in a new commit on top, then make three more note commits. Use git bisect run with the old and new terms --term-old=without --term-new=with to find the commit that added "Exam fee". Your test must exit with 0 when the line is missing.

What does Git print? Before you open the solution, decide what the test script must contain, and which commit bisect will name.

Show solution
Terminal
echo "Exam fee: 800" >> fees.txt
git commit --quiet -am "Add the exam fee"
for i in 11 12 13; do echo "Note $i" >> notes.txt; git commit --quiet -am "Update notes ($i)"; done
cat > ../no-exam-fee.sh <<'EOF'
#!/bin/sh
! grep -q "Exam fee" fees.txt
EOF
chmod +x ../no-exam-fee.sh
git bisect start --term-old=without --term-new=with HEAD HEAD~4
git bisect run ../no-exam-fee.sh
git bisect reset
Output
Bisecting: 1 revision left to test after this (roughly 1 step)
[a344a662778552b770ad6a158847b0fcc1c2e74b] Update notes (11)
running '../no-exam-fee.sh'
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[d822d742e9aef78e4c6481f94384551695c22fbc] Add the exam fee
running '../no-exam-fee.sh'
d822d742e9aef78e4c6481f94384551695c22fbc is the first with commit
commit d822d742e9aef78e4c6481f94384551695c22fbc (HEAD)
Author: Navika Anamaneni <navika@example.com>
Date:   Mon Jan 5 11:26:00 2026 -0600
 
    Add the exam fee
 
 fees.txt | 1 +
 1 file changed, 1 insertion(+)
bisect found first bad commit
Previous HEAD position was d822d74 Add the exam fee
Switched to branch 'main'

The ! reverses grep's result, so the script exits with 0 (old, "without") while the line is missing. Bisect named "Add the exam fee" as the first commit "with" it. Custom terms make bisect useful for any change, not only bugs.

Checkpoint. In a Java project with tests, find a commit that broke a test with git bisect run mvn -q test. If you have no such project, build ten commits by hand in a practice repository, break something in one, and find it with both manual and automatic bisect.

Quick check: Git bisect

4 questions
1What do you need to start a bisect?
2About how many tests does bisect need for 1,000 commits?
3In git bisect run, what does exit code 0 mean?
4How do you leave bisect and go back to your branch?

Key Takeaways

Quick reference

CommandIt is used to
git bisect start <bad> <good>Begin a search
git bisect good / badAnswer for the current commit
git bisect skipSkip an untestable commit
git bisect run <command>Search automatically
git bisect logSee the answers so far
git bisect resetEnd the search

Was this tutorial helpful?

Lesson 67 of 106