When something worked last month and does not now, staring at the diff is the slow way. git bisect does a binary search through your history: you name one good commit and one bad one, and git checks out the middle for you, again and again. With a thousand commits in between it needs about ten steps. The part I would not skip is git bisect run, which lets a script do the testing, so you can go and make coffee. You need a git repository and any way to tell good from bad, a test or even a one-line command.
- Time
- 3 min
- Steps
- 5
- Updated
- 19 Nov 2026
You will learn
- Start the search
- Let a script do it
- A script for a bug without a test
- Finish properly
- Limits
Step 1 of 5: Start the search
First make sure your working tree is clean (commit or stash), because bisect checks out old commits. Then:
git bisect start
git bisect bad # the current commit is broken
git bisect good v1.4.0 # a tag, hash or branch where it still worked
Git answers with the number of steps it expects and checks out a commit in the middle. Test it however you test, then tell git what you saw:
git bisect good # this one works
git bisect bad # this one is broken
Repeat until git prints that the first bad commit is found, with its hash and message. If a commit cannot be tested at all, say it does not build, use git bisect skip and git picks a neighbour.
Step 2 of 5: Let a script do it
Doing this by hand gets old at step six. Write a command that exits 0 when the code is good and non-zero when it is bad, and hand it over:
git bisect start HEAD v1.4.0
git bisect run python -m pytest -q tests/test_invoice.py
git bisect start <bad> <good> is the short form of the three lines above. According to the git manual the script must exit with 0 for good and with a code between 1 and 127, except 125, for bad. Exit code 125 means “cannot test this commit, skip it”, and any other code above 127 aborts the whole run, so a crashing script does not quietly give you a wrong answer.
Step 3 of 5: A script for a bug without a test
You do not need a test suite. A shell one-liner is fine:
git bisect run sh -c 'make -s && ./build/app --check-config'
If the build itself fails on some commits, wrap it so that a failed build returns 125 instead of 1, or you will blame a commit for a build error that has nothing to do with your bug:
git bisect run sh -c 'make -s || exit 125; ./build/app --check-config'
Step 4 of 5: Finish properly
git bisect log # what you answered, handy for a bug report
git bisect reset # go back to where you started
reset returns you to the commit you had checked out before start. Forgetting it is the classic mistake: you end up editing code on a detached head from months ago. Then run git show <hash> on the culprit and read what it changed.
Step 5 of 5: Limits
Bisect assumes the bug appeared once and stayed. If a bug comes and goes, the answer is meaningless, so make the test deterministic first. It also works best with small commits: a result pointing at a 3,000-line squash merge is correct and useless.
