Something broke and you don't know which commit? git bisect finds it

git bisect runs a binary search between a good commit and a bad one: each step halves what is left until it reaches the first commit that broke things. With git bisect run, a script tests each step for you.

Published 3 min read#Developer tools#Web development

The feature worked in last month's version, today it is broken, and there are a hundred commits in between. You can open them one by one, or you can let Git play a game of "higher or lower" with you and reach the guilty commit in a few steps. This article shows how to run git bisect by hand, and how to let it run on its own.

What is git bisect?

git bisect is a Git command that finds the first commit that broke something, with a binary search between a point where it worked and a point where it is broken. Each step cuts the remaining commits in half.

The official git bisect documentation defines it in one line: "Use binary search to find the commit that introduced a bug".

Binary search is like the game "think of a number between 1 and 100". You do not ask "is it 1? is it 2?", you ask "is it bigger than 50?", and every answer throws away half of the numbers.

Why does searching by hand take so long?

Searching by hand takes long because you open the commits in order, and each one needs a build and a test. The easy mistake is to start from the latest commit and walk back, so if the bug is old, you go through everything before you reach it.

git bisect flips the method: instead of walking, you jump to the middle. Each answer from you ("good" or "bad") throws away half of the road that is left.

How do you run git bisect by hand?

You run git bisect with three commands: start the session, say the current version is bad, and say which version was good. This is the example from the documentation itself:

git bisect start
git bisect bad                 # Current version is bad
git bisect good v2.6.13-rc2    # v2.6.13-rc2 is known to be good

Git then moves you to a commit in the middle. Test the feature, and tell it the result:

  1. If it works, type git bisect good.

  2. If it is broken, type git bisect bad.

  3. If this commit cannot be tested at all (it does not build, for example), type git bisect skip, and Git picks a nearby one instead.

You repeat this step until Git prints the first bad commit. At the end, type git bisect reset, which takes you back to the commit you were on before git bisect start.

How do you let it test on its own?

If you have a command that can tell whether the code is good or bad, like a single test, git bisect run runs it at every step and reaches the answer without you sitting there. The script talks to Git through its exit code:

Exit code

What it means to git bisect

0

The code is good

1 to 127, except 125

The code is bad

125

Cannot be tested, skip it

Any other code

Stop the session

So if you have a test that fails only on the bug:

git bisect start
git bisect bad HEAD
git bisect good <good-commit>
git bisect run npm test -- checkout.test.js
git bisect reset

Watch out for 125: if the project build itself fails on some commits, make the script exit with 125 in that case, not 1, so Git does not count the broken build as the bug you are looking for.

Where do you start now?

The lesson for you as a developer: when you do not know where the bug is, do not read the commits in order, cut the road in half, because every answer throws away half the work.

  1. Find a tag or an old commit where you are sure the feature worked.

  2. Write a small test or command that fails only on the bug.

  3. Run git bisect start, bad and good, then git bisect run with the test.

  4. When the commit shows up, read its change, and type git bisect reset.

If you found the commit and want to undo it after it reached the shared branch, we wrote about how to undo a pushed commit. All our developer tools articles are in one place: developer tools articles. And if you are still building your basics, the 2026 programmer roadmap puts what to learn in order.

The last time you hunted for a commit that broke something, how long did it take you?

FAQ

What if a commit in the middle does not build and I cannot test it?

Type git bisect skip, and Git picks a nearby commit instead. If your script does the testing, make it exit with code 125 for such a commit.

How do I get back to where I was when I am done?

git bisect reset takes you back to the commit you had checked out before git bisect start.

Can git bisect find something that is not a bug, like a speed change?

Yes. The Git docs say you can use the terms old and new instead of good and bad, when you look for the commit that changed one state into another. You cannot mix the two pairs in one session.

Share

More articles