Debugging with git bisect

Last week, I had to debug a regression in a codebase.

Something was working at the last release but is now broken.

There have been around 20 commits to the mainline branch since the last release, and the first step to fixing the issue is to determine which commit caused the regression.

Git has a great tool for this - git bisect.

You tell Git what the last known working commit was, such as the tag of the last release, and it will start to split the commits and prompt you to tell it whether the commit is good or bad.

If there are 20 commits, it may pick commit number 10, and based on whether the commit is good or bad, it may pick commit 5 or 15.

Based on your answers, Git will then tell you which the first bad commit is.

Even better, if it's something that you can script or is covered with an automated test, git bisect can run a command for you and find the failure automatically rather than a human needing to check manually.

Once you've found the commit that breaks, you can view it and find and fix the bug.

- Oliver

P.S. Need help or want another pair of eyes on your code? Book a 1-on-1 consulting call or an online pair programming session with a 100% money-back guarantee.,

Was this useful?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.