Automatically Locate When and Where Bugs Were Introduced with git bisect

git bisect start
git bisect bad
git checkout $(git rev-list --max-age=2016–06–03 --date-order --reverse HEAD | head -n 1)
git bisect good
Bisecting: 987 revisions left to test after this (roughly 10 steps)
[8fe7a35242303e8316c1d77883c0e247db6df5a8] testDefaultTotalCountIsZero
Bisecting: 489 revisions left to test after this (roughly 9 steps)
[03c4c4e5bd1740a277a606dd208e17a855761243] Merge branch 'release/1.5' of https://github.com/elliotchance/concise into 1.4/193-set-color-scheme
57a00e86622073c887be101a2e76c079245e519c is the first bad commit
git show 57a00e8
git diff 57a00e8 57a00e8^
git bisect reset

Gotchas

You will find that the rinse-and-repeat process above works most of the time, but there are a few little tricks for when it goes wrong.

  1. git bisect is very clever in that it will isolate the commit even if it’s not a completely linear ancestry. If you know several commits that are good or bad going in you can checkout and confirm these individually at any time without affecting progress (or hopefully lessening it).
  2. Each git bisect requires a git checkout which requires a clean working directory. It is common to have to tweak the code at each step to verify your bug. However, you will have to return the code to a clean checkout state before proceeding. This is especially annoying if you need to apply and revert the same patch for each step. For this I would recommend git stash.

For Lazy People

If your test is something that can verified through the console, such as finding when a unit test started to fail. You can use git bisect run to automate the process. git bisect run will use the exit status on the command you give it to decide if the commit was good or bad. Here is an example:

git bisect run phpunit --filter testRecordIsSavedToTheDatabase

--

--

I’m a data nerd and TDD enthusiast originally from Sydney. Currently working for Uber in New York. My thoughts here are my own. 🤓 elliotchance@gmail.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store