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.