• Ian Stuart's picture

    The awesomeness that is `git bisect`

    Ian Stuart / December 17, 2018
  • So you've been working away on some code, and it works great. You then rebase on master, and it breaks.

    aarrgghhhh!!!! Steam-from-the-ears!!! pulling-of-hair!!!

    Until now, I'd either be checkoing out one branch at a time & testing, or doing some kind of cherry-picking into a new branch - either way, it's a pain keeping track of what's working & what's not.

    Enter git bisect

    git bisect (https://git-scm.com/docs/git-bisect) basically does the checkout & mark good/bad stuff for you, and walks you to the first bad commit.

    This i s the git log for a recent example of where the change has broken:

        commit c57ae4a1ff73d066d74d12252345a3f8e21bbb1d (HEAD -> WIP-add_collect, origin/WIP-add_collect)
        Author: Ian Stuart 
        Date:   Fri Dec 14 11:24:05 2018 +0000
    
            re-formatted
    
        commit 9848dd9425c458f4bfdede8f2e59ab6b5ffcac87
        Author: Ian Stuart 
        Date:   Fri Dec 14 11:19:06 2018 +0000
    
            Finish switching [almost] everything to f-strings
    
        commit 460e925bcb93ce4145b86c83daf7da39aa46b99d
        Author: Ian Stuart 
        Date:   Fri Dec 14 10:50:32 2018 +0000
    
            First set of mods to move to f-strings
    
        commit 14b3668d8a7bd628a7aa444e3ea4a09d0ff1f82e
        Author: Ian Stuart 
        Date:   Fri Dec 14 10:35:33 2018 +0000
    
            Centralise the whole parameter collection & parsing code
    
        commit a2035d3c5ce3d1a81bc42349a47d86a01845b805
        Author: Ian Stuart 
        Date:   Fri Dec 14 09:05:08 2018 +0000
    
            Add in the Collect method to download a submission
    
        commit 8b271fd3be6ad87b4ed89df56eebcf41565dfe64 (origin/master, origin/HEAD, master)
        Merge: 035a60e 7f43ad4
        Author: Bert Robberechts 
        Date:   Mon Dec 10 14:21:34 2018 +0000
    
            Merge branch 'WIP-add_collect' into 'master'
        
            Interrum store for add_collection code
        
            See merge request naas/nbexchange!10
    

    To run the bisect process, I ensure I've checkout out my broken branch, and then setup the bisect process:

        git checkout WIP-add_collect
        git bisect start
        git bisect good 8b271fd3be6ad87b4ed89df56eebcf41565dfe64
        git bisect bad c57ae4a1ff73d066d74d12252345a3f8e21bbb1d
    

    good is the commit-id for master, and bad is for the HEAD of my branch

    Git bisect then automatically checks out a mid-point branch. You can build images, run tests, and/or do any of the things you need to do to test whenther the code works of not. - for me, this state was working

        git bisect good
    

    This marks that commit as good, so git bisect jumps forward and checks out a mid-point commit. Repeat tests et-al - for me, this state was broken

        git bisect bad
    

    Mark this as a broken state.

    Repeat as needed until git bisect finds the first known bad commit:

        git bisect bad
        460e925bcb93ce4145b86c83daf7da39aa46b99d is the first bad commit
        commit 460e925bcb93ce4145b86c83daf7da39aa46b99d
        Author: Ian Stuart <ian.stuart@example.com>
        Date: Fri Dec 14 10:50:32 2018 +0000
    
                   First set of mods to move to f-strings
    
        :040000 040000 aa6f0aadb4546b175b2381aa79bbbb4bb842f7c7 cc67c02c4a57c0d2487b5f5c33085e47f4512e12 M nbexchange
    

    Now I'm on this branch, I can compare the changes I've made, or checkout the preceding branch & start a new branch with new changes.

    So, so much easier than having to remember commit numbers, remember where you've been, and which ones worked, and didn't.

    My personal preference at this point is to check out the last working commit, start a new branch, and make code-changes from there - Your Milage May Vary

    git bisect reset will revert to the original commit on the original branch.