Posted over 8 years ago. Visible to the public. Repeats. Linked content.

How to: Use git bisect to find bugs and regressions

Git allows you to do a binary search across commits to hunt down the commit that introduced a bug.

Given you are currently on your branch's HEAD that is not working as expected, an example workflow could be:

Copy
git bisect start # Start bisecting git bisect bad # Tag the revision you are currently on (HEAD) as bad. You could also pass a commit's SHA1 like below: git bisect good abcdef12345678 # Give the SHA1 of any commit that was working as it should # shorthand: git bisect start <bad ref> <good ref>

Git will fetch a commit between the given good and bad commits.

You could then run a test (*) revealing the problem or inspect the issue manually. Keep in mind that you may need to migrate your database as Git is unaware of Rails and will not cast any magic.

If Git checked out a commit that is working:

Copy
git bisect good

If the current commit is bugged:

Copy
git bisect bad

Now, another commit will be checked out. See if the issue persists and call git bisect good or git bisect bad, resulting in another commit being fetched and so on.

Do this until Git reveals the commit which first contained the bug:

Copy
abc1234def5678 is the first bad commit commit abc1234def5678 Author: John Doe <user@example.com> Date: Thu Dec 23 13:37:00 2010 +0100 make everything better

Once you are done bisecting you can go back with

Copy
git bisect reset

(*) Such a test would need to live outside the repository as you are constantly switching your working directory's state. Also, make sure to cover wording changes etc.

If you do, you can also have git find the bad commit automatically by using git bisect run.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
over 1 year ago
by Dominik Schöler
Keywords:
git-bisect, guide, debug, debugging
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more