What is git bisect?
git bisect finds the commit that broke something. You tell Git one “bad” commit (where the bug exists) and one “good” commit (where it worked). Git checks out a commit in the middle, you test it and mark it, and Git halves the range each time.
It can even run a test script for you with git bisect run. In this example, a file starts to contain the word BUG in some commit:
The test script test.sh fails (exit code 1) when the file has BUG:
#!/bin/sh
if grep -q BUG status.txt; then exit 1; fi
exit 0
$ git bisect start
status: waiting for both good and bad commits
$ git bisect bad HEAD
status: waiting for good commit(s), bad commit known
$ git bisect good HEAD~7
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[c9f3864712df8e8c2a36c5713f79b4886088788e] Commit 4
$ git bisect run ./test.sh
running './test.sh'
Bisecting: 1 revision left to test after this (roughly 1 step)
[ec5695fc6c5e58f2148f2bee260b170d5bd4c2bf] Commit 6
running './test.sh'
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[ff72f6258a3d508a43c3e2b650038aaf9b6797aa] Commit 5
running './test.sh'
ff72f6258a3d508a43c3e2b650038aaf9b6797aa is the first bad commit
commit ff72f6258a3d508a43c3e2b650038aaf9b6797aa (HEAD)
Author: Robin Sharma <robin@example.com>
Date: Sat Jan 10 11:03:00 2026 +0000
Commit 5
status.txt | 1 +
1 file changed, 1 insertion(+)
bisect found first bad commit
$ git bisect reset
Previous HEAD position was ff72f62 Commit 5
Switched to branch 'main'
Git found “Commit 5” as the first bad commit. git bisect reset brings you back to the original branch.
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |