Unbreak Your Code With Git Bisect

git bisect is a helpful tool to assist you in finding which commit introduced a bug.

In a world without git bisect when you need to find the commit that introduced a bug you’d likely have to go through commits one by one, manually checking each one out and then doing some testing. git bisect can make that process a bit less painful by automating a binary search of your commits.

To start it just execute git bisect start.

The latest commit likely has the bug so mark it as bad: git bisect bad.

Now give it a good commit before the bug was introduced: git bisect good 7df148d3.

git bisect will now cycle through the commits. For each one you’ll need to mark each one as good or bad. Then it’ll move on to the next commit.

Here is an (abbreviated) example below:

» git bisect start

» git bisect bad

» git bisect good 7df148d3
Bisecting: 35 revisions left to test after this (roughly 5 steps)

» git status
HEAD detached at 4f5d1140
You are currently bisecting, started from branch 'master'.
  (use "git bisect reset" to get back to the original branch)

» git bisect bad
Bisecting: 17 revisions left to test after this (roughly 4 steps)

» git bisect good
Bisecting: 5 revisions left to test after this (roughly 2 steps)

» git bisect bad
f01f1b96feed92b6afd60ebac576571a98bcda56 is the first bad commit
commit f01f1b96feed92b6afd60ebac576571a98bcda56
Author: Joe Dev <jdev@companyfoo1000.com>
Date:   Tue Jan 16 15:40:46 2018 -0600

    Add super awesome feature that won't break anything

Each time you mark a commit as good/bad it moves to a new commit. You can see the git status above shows the current commit. After the last git bisect bad it identified the commit that introduced the bug.

With the commit found just clean up with git bisect reset and you are good to go!

There are quite a few other options with git bisect that you can read about in the git docs.