Git Bisect is awesome
So I was attempting to debug a weird bug that cropped up in a release branch that hadn't existed before all the original feature branches were merged. There were quite a few feature branches and changes so I was scratching my head trying to figure out what broke it.
Enter Scene: Ben Holley (Isotope11 Developer)
"You should try git bisect! I've never done it but I heard it will help you here"
Turns out its awesome. It will step between a known good commit/branch and a known bad commit/branch and allow you to test your code in between and mark them as good or bad. Eventually, and fairly quickly you will narrow down what commit caused your particular problem. Here is how:
$ git bisect start $ git bisect good some_good_branch $ git bisect bad master > Bisecting: 60 revisions left to test after this (roughly 6 steps)
You will now be in a "bisect" branch that is somewhere between the known good point and the known bad point. At this point you run whatever test you need to and verify if your code works at this point or not.
Lets assume it does not
$ git bisect bad > Bisecting: 29 revisions left to test after this (roughly 5 steps)
Git has stepped us to a different point in the tree to test here. Notice that it says that there are only 29 revisions left to test. It doesn't just step back one by one it figured out what branches definitely can't have caused the problem based on the tree. So you don't have to step through every single commit. In this case git estimates I'll have roughly 5 steps left.
Basically you will continue through the git bisect good or bad routine until eventually git returns this
> db791e5031d72dea6a6cfb761d349c53277c1a4f is the first bad commit
You now know exactly which commit broke your code.
To return to the branch you were in when you started the git bisect type:
$ git bisect reset
Now use git show to figure out what was in that commit that was so horrible and you're done!
$ git show db791e5031d72dea6a6cfb761d349c53277c1a4f
This saved me hours of debugging time, hopefully it will help someone out there too
Adam has worked with Isotope 11 for 4 years and has been a professional software developer for over 12 years. He has been lead developer on multiple Fortune 500 projects. He is the author of "Beginning Rails 4" which was published by Apress in September 2013.