Debugging With Git Bisect

You can use

git bisect

to find out which commit caused a bug.

An example repository

In a nice open source example, I found an arbitrary exemplar on github

import os

top_dir = os.getcwd()
git_dir = os.path.join(top_dir, "learning_git")
os.chdir(git_dir)
%%bash
rm -rf bisectdemo
git clone https://github.com/shawnsi/bisectdemo.git
Cloning into 'bisectdemo'...
bisect_dir = os.path.join(git_dir, "bisectdemo")
os.chdir(bisect_dir)
%%bash
python squares.py 2 # 4
4

This has been set up to break itself at a random commit, and leave you to use bisect to work out where it has broken:

%%bash 
./breakme.sh > break_output
error: branch 'buggy' not found.
Switched to a new branch 'buggy'

Which will make a bunch of commits, of which one is broken, and leave you in the broken final state

python squares.py 2 # Error message
  Input In [6]
    python squares.py 2 # Error message
           ^
SyntaxError: invalid syntax

Bisecting manually

%%bash
git bisect start
git bisect bad # We know the current state is broken
git checkout master
git bisect good # We know the master branch state is OK
status: waiting for both good and bad commits
status: waiting for good commit(s), bad commit known
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
Bisecting: 500 revisions left to test after this (roughly 9 steps)
[5b7aa71ece8b0c32fd52dfa04d52ae0a7c7893cc] Comment 500

Bisect needs one known good and one known bad commit to get started

Solving Manually

python squares.py 2 # 4
git bisect good
python squares.py 2 # 4
git bisect good
python squares.py 2 # 4
git bisect good
python squares.py 2 # Crash
git bisect bad
python squares.py 2 # Crash
git bisect bad
python squares.py 2 # Crash
git bisect bad
python squares.py 2 #Crash
git bisect bad
python squares.py 2 # 4
git bisect good
python squares.py 2 # 4
git bisect good
python squares.py 2 # 4
git bisect good

And eventually:

git bisect good
    Bisecting: 0 revisions left to test after this (roughly 0 steps)

python squares.py 2
    4

git bisect good
2777975a2334c2396ccb9faf98ab149824ec465b is the first bad commit
commit 2777975a2334c2396ccb9faf98ab149824ec465b
Author: Shawn Siefkas <shawn.siefkas@meredith.com>
Date:   Thu Nov 14 09:23:55 2013 -0600

    Breaking argument type
git bisect end

Solving automatically

If we have an appropriate unit test, we can do all this automatically:

%%bash
git bisect start
git bisect bad HEAD # We know the current state is broken
git bisect good master # We know master is good
git bisect run python squares.py 2
Previous HEAD position was 5b7aa71 Comment 500
Switched to branch 'buggy'
status: waiting for both good and bad commits
status: waiting for good commit(s), bad commit known
Bisecting: 500 revisions left to test after this (roughly 9 steps)
[5b7aa71ece8b0c32fd52dfa04d52ae0a7c7893cc] Comment 500
running  'python' 'squares.py' '2'
4
Bisecting: 250 revisions left to test after this (roughly 8 steps)
[dc414c0a9845484dd998a6b40c08ce3154849056] Comment 750
running  'python' 'squares.py' '2'
4
Bisecting: 125 revisions left to test after this (roughly 7 steps)
[e3222fb68410b5d3f55f7f178c8bbdedc4dca342] Comment 875
running  'python' 'squares.py' '2'
4
Bisecting: 62 revisions left to test after this (roughly 6 steps)
[07b4dead688d6e2c57af8a52043cf378673cb248] Comment 938
running  'python' 'squares.py' '2'
4
Bisecting: 31 revisions left to test after this (roughly 5 steps)
[6d4f372a31d8ef2323b1f69385cb144b283ffdea] Comment 969
running  'python' 'squares.py' '2'
4
Bisecting: 15 revisions left to test after this (roughly 4 steps)
[56012d1208030be0444c42562847c12b20f817e5] Breaking argument type
running  'python' 'squares.py' '2'
Traceback (most recent call last):
  File "squares.py", line 9, in <module>
    print(integer**2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
Bisecting: 7 revisions left to test after this (roughly 3 steps)
[8e9a5953b9f16b428d8b3f9aa6bb2f3e656347fd] Comment 977
running  'python' 'squares.py' '2'
4
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[fa8278ea3b941537d421bde5a9941029f250128b] Comment 981
running  'python' 'squares.py' '2'
4
Bisecting: 1 revision left to test after this (roughly 1 step)
[caf0b058cc51591815fbcaba4dfe79d7e800a9be] Comment 983
running  'python' 'squares.py' '2'
4
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[5bf0d7554e8dad75aec9e54a04dae08faa57573a] Comment 984
running  'python' 'squares.py' '2'
4
56012d1208030be0444c42562847c12b20f817e5 is the first bad commit
commit 56012d1208030be0444c42562847c12b20f817e5
Author: Shawn Siefkas <shawn.siefkas@meredith.com>
Date:   Thu Nov 14 09:23:55 2013 -0600

    Breaking argument type

 squares.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
bisect found first bad commit

Boom!