git bisect is an indispensable tool for fixing bugs. It can be an extremely efficient way to identify a change that broke your code and therefore find a fix for your problem.
git bisect does its magic with a simple binary search. You start by specifying a commit where the code is broken (with
git bisect bad) and a commit where the code works (
git bisect good).
git bisect will automatically check out a commit between those two commits. You test the code again, mark it as good or bad, and
bisect will check out another commit. Eventually, you’ll narrow down to the commit when the code broke, and then you can fix the bug yourself (or identify who else broke the code, and get them to fix it).
My favorite way to run
git bisect is with the
run parameter. If you have a script
./script.sh that can test if the code is good or bad, and it exits with
0 for good and anything else for bad, then you can use
git bisect run ./script.sh – it’ll automatically use the script’s output to mark commits as good or bad, and will quickly identify the breaking change.
There are a lot of finer points to using
git bisect, as detailed in the official documentation, and I’ve glossed over some details, but I’ve never needed more than this basic usage. I hope it’ll make your coding life happier. Enjoy!