Git Bisect and You

Fri, May 27, 2016 3-minute read

git bisect can be extremely helpful for tracking down changes that were created, but you’re not exactly sure where or when. An example– I tried to create a new feature on an existing page, and after clicking a button, a modal window opened and immediately closed. I knew it used to work properly at some point, but it wasn’t now.

The basics

git bisect will take 2 given commits: 1 “good” working one, and 1 “bad” broken one. It will then take the median of the two, and wait for you to tell it if it is “good” or “bad”. Once you continue to label each try as “good” or “bad”, you arrive shortly at the end of all possible commits, and it will tell you what the first bad commit was.

Where to start

To find the first “good” one, you can look at your git log, and pick an entry from several days ago, when you think it was working.

$ git log

0ee794a - (davebrown/bug/fix-utc-issue) Fix missing times (2 days ago) <Dave Brown>
72e190a - (davebrown/develop) Merge pull request #1450 from johnson/feature/new-wizard (5 days ago) <Julie Smith>
813ace0 - resolved merge conflicts (5 days ago) <Ted Johnson>
dfbbaf6 - Merge pull request #1527 from juliesmith/feature/workflow (6 days ago) <Julie Smith>
af2ecea - Add Waiting on Status (6 days ago) <Julie Smith>
3a69747 - Merge pull request #1525 from katyjohnson/feature/admin-changes (6 days ago) <Julie Smith>
a348fc8 - Merge pull request #1524 from williams/fix/serializer (6 days ago) <Julie Smith>
25bb7b1 - Fix tests and HTTP codes (6 days ago) <Adam Williams>
3cae9b1 - Add improvements to admin (6 days ago) <Katy Johnson>

So you can check it out directly:

$ git checkout 3cae9b1

Then reload your page and see if your issue works. If it does, great. This will be your “good” commit. If it still doesn’t work, checkout a previous commit and test it until you find one that works properly. Then take the most recent one (0ee794a in this case) and that will be your “bad” one.

Bisecting

Git bisect must be invoked at the top level directory, with

$ git bisect start

followed by tagging the good commit appropriately:

$ git bisect good 3cae9b1

followed by the known bad commit as well:

$ git bisect bad 0ee794a

and it will bisect and tell you what commit you are on. Reload your page and see if it works. If it does, we continue:

$ git bisect good

and it brings us to another commit. We reload our browser, and we see the site– and it fails. So we mark that as:

$ git bisect bad

We get another commit, and reload our browser. It works!

$ git bisect good

We do this until git finally responds with

first known bad commit -- 813ace087f52b251ed6372fa2363e48e4e1680d7

Finding and fixing the bug

So now we know where a bug was introduced. We can go directly to this in github with the link:

https://github.com/<github_user>/<github_repo>/commit/813ace087f52b251ed6372fa2363e48e4e1680d7

It is in this commit where things went south. Note the new changes and diagnose where the problem lies. Since we’re done with git bisect, we say:

$ git bisect reset

and now you’re ready to create a new branch or check out an existing one.