Last Updated: February 25, 2016
·
932
· jorge-d

How-to Git bisect

Let's pretend you are working with collaborators in a project, and after a git pull you notice that the some cukes/specs are not passing anymore.

Well, the good news is that you can easily find the commit that introduced this fail using git bisect.

First, you need to be on a clean repository.
If you have any local modification use the command

$ git stash

Now, we can begin the bisection !

$ git bisect start
$ git bisect bad

Let's pretend that the revision dca6dd7517 was the last version tested that was good

$ git bisect good dca6dd7517
Bisecting: 13 revisions left to test after this (roughly 4 steps)

The git-bisect puts you then in a specific commit and you can run your cukes/specs to see if this revision contains the fail you are looking for and mark the revision as good or bad

$ rspec
[...]
$ git bisect good # or bad
Bisecting: 6 revisions left to test after this (roughly 3 steps)

And so on until there's no remaining step.
You'll get this message

<revision> is the first bad commit

Now you know which commit is the guilty one :)

Once you have finished the bisection, simply run

$ git bisect reset

Last but not least

At any point, you can see the progress of the bisection

$ git bisect log

You can skip to mark a specific commit as good or bad by typing

$ git bisect skip

If you stashed your changes at the beginning, don't forget to pop it

$ git stash pop