Finding the bad commit with Git

a knight in shining armour
I recently found a problem in my codebase and realised it had been around for a while. A few weeks ago everything worked fine and I didn’t know when or why it started to go bad. If only I knew when the bug was introduced I could save myself a great deal of debugging. It was then that I learned about Git Bisect which turned out to be my knight in shining armour.

Git Bisect

Git Bisect finds the first bad commit if you have a good starting point and a bad ending point. Instead of narrowing down the interval manually yourself, Git Bisect will fetch a commit in the middle of the interval and let you try it out. Each iteration will halve the search interval until the faulty commit is found.

Depending on the type of bug you are dealing with you can either use the semi-manual or the automatic approach.

The Semi-Manual Approach

In this example we know that commit f890d22 is good and bug free. We don’t know when the error was introduced but we know that it exists in commit d0d56aa.

To start bisecting, write git bisect start together with the good and bad commit:

When you have tested a commit provided by Git, you should provide one of the feedback commands below to move on to the next step.

This is how it looks in the terminal.


In each step you will see roughly how many iterations are left until finally:


The first bad commit is found. It was commit 3dcf49dHappiness!

End the session with git bisect reset when you are done bisecting.

The Automatic Approach

If you are lucky enough to look for a bug that can be found with a script, you can let Git Bisect search the interval for you. The following exit codes will let Git know what to do.

The script file should be saved outside of the repository so that Git doesn’t treat it as an untracked file. It could be a script that looks for suspicious comments in a file, builds your project, runs unit tests – or as in my case below, checks if a file is missing.

The automatic approach is initiated with git bisect run together with the path and filename.

The terminal output:


End the session with git bisect reset when you are done bisecting.

Happy debugging!

This Post Has One Comment

  1. Hi,nice article but i want to find out exactly how the bisect command is able to identify a bad commit.I know it uses a binary search technique.But i want to know how git is able to detect the bugs.

Leave a Reply

Close Menu