🐳
Published on

Use git bisect to see when you broke the tests

Authors

I wanted to push a number of commits but my tests failed. Naughty me for not running them more often. Anyway, here's how to use git bisect to tell when they started failing.

git bisect start HEAD origin/main
git bisect run npm test

You should get a result something like:

b73399a404433af29d95fb66d7b7facc1de49aa3 is the first bad commit

And then you can run:

git bisect reset

when you're done to exit bisect mode and go about fixing things.

A bit more explanation

Here are some details if you want them.

Start

git bisect start HEAD origin/main

The syntax here is git bisect start <known-bad-commit> <known-good-commit>.

In the example above I've used HEAD as a known bad commit, because it's the latest commit. My example assumes the remote is called origin and the branch is main.

You could narrow the range further, but this will work well if your tests are fast enough, and you don't have too many commits.

Run

git bisect run npm test

The syntax here is git bisect run <command>, where command will exit with 0 if all is well and a non-zero code if not. (Read the docs if this doesn't work because there are some exceptions, but should be a fine assumption in most cases.)

If you don't have an automated way of telling whether things are fine, you can also do your testing manually. Run git bisect bad or git bisect good to mark commits as good as bad. Git will walk you through it.

Sources