Find where your code broke with git bisect

Your code is broken. You know everything was working at a certain point in time or that a particular commit was ok, but you're not sure which commit since then has introduced the problem.

You need git bisect.

From the git documentation.

git-bisect - Find by binary search the change that introduced a bug

Umm, right. Ok.

A git bisect starts by specifying a good and a bad revision. git will then checkout each commit by splitting (or bisecting) the range of commits and running the supplied command until it returns with an exit code of zero.

An exit code of zero means the command was successful.

The command can be anything that will return an exit code, which includes most *nix binaries or scripts.

In this example, we're using Cucumber as the command.

A working example using Cucumber

1
2
3
4
git bisect start
git bisect good <commit-where-stuff-is-working>
git bisect bad <commit-where-stuff-is-not-working>
git bisect run bundle exec cucumber features/a-feature.feature

If the tests fail, Cucumber will return a non-zero exit code and git will keep trying other commits until it finds one that is successful. We will then know when the code was "good", and more importantly, the commit where it became "bad".

When finished, reset your branch to the previous state before starting the git bisect.

1
git bisect reset

As you'd expect with git, it can do a lot more than this, but this is the way in which I normally use it.