Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Debugging with Git

Debugging with Git

Lightning talk on how to use git bisect to debug your code. Presented at
• DDDNorth 2019,
• dotnetsheff June 4th 2019
• UIKonf (remotely) May 18th 2020
• Sky Betting & Gaming Engineering Conference Feb 2023

For more info see here:
https://git-scm.com/docs/git-bisect

Luke Stringer

March 02, 2019
Tweet

More Decks by Luke Stringer

Other Decks in Programming

Transcript

  1. © AND Digital 2023
    Luke Stringer, Tech Principal @ AND Digital
    Debugging with Git

    View full-size slide

  2. © AND Digital 2023

    View full-size slide

  3. © AND Digital 2023
    Given some steps to reproduce…
    Approaches to Debugging
    1. Manually look for the problem using
    • Intuition
    • Print statements & Breakpoints
    • Remote logging
    2. Find when the bug was introduced

    View full-size slide

  4. © AND Digital 2023
    2. Find when the bug was introduced
    How?

    View full-size slide

  5. © AND Digital 2023
    Use your Git history

    View full-size slide

  6. 598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b

    View full-size slide

  7. Bug!
    598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b

    View full-size slide

  8. Bug!
    598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    Find the commit where the
    bug was introduced
    • What changed
    • Why it was changed
    • Who changed it

    View full-size slide

  9. © AND Digital 2023
    $ git bisect
    • Automates the process using a binary search.
    • Very ef
    fi
    cient; worst case scenario O(log n)
    • 10 commits takes 4 steps. log2(10)
    • 100 commits takes 7 steps. log2(100)
    • 1000 commits takes 10 steps. log2(10)

    View full-size slide

  10. © AND Digital 2023
    $ git bisect
    • You provide 2 control points:
    • A “good” commit, e.g. bug absent
    • A “bad” commit, e.g. bug present Bad
    Good

    View full-size slide

  11. 598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    © AND Digital 2023

    View full-size slide

  12. 598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b

    View full-size slide

  13. 598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    $ git bisect start

    View full-size slide

  14. Bad
    598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    $ git bisect start
    $ git bisect bad

    View full-size slide

  15. Bad
    598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    $ git bisect start
    $ git bisect bad
    $ git bisect good 598fde5

    Good

    View full-size slide

  16. Bad
    598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    $ git bisect start
    $ git bisect bad
    $ git bisect good 598fde5

    Bisecting: 4 revisions left 

    to test after this 

    (roughly 2 steps) [6f5e527]
    Good

    View full-size slide

  17. Bad
    598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    $ git bisect start
    $ git bisect bad
    $ git bisect good 598fde5

    Bisecting: 4 revisions left 

    to test after this 

    (roughly 2 steps) [6f5e527]
    $ git bisect bad

    Good

    View full-size slide

  18. Bad
    598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    $ git bisect start
    $ git bisect bad
    $ git bisect good 598fde5

    Bisecting: 4 revisions left 

    to test after this 

    (roughly 2 steps) [6f5e527]
    $ git bisect bad

    Bisecting: 2 revisions left 

    to test after this 

    (roughly 1 step) [97b914a]
    Good

    View full-size slide

  19. Bad
    598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    $ git bisect start
    $ git bisect bad
    $ git bisect good 598fde5

    Bisecting: 4 revisions left 

    to test after this 

    (roughly 2 steps) [6f5e527]
    $ git bisect bad

    Bisecting: 2 revisions left 

    to test after this 

    (roughly 1 step) [97b914a]
    $ git bisect good

    Good

    View full-size slide

  20. Bad
    598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    $ git bisect start
    $ git bisect bad
    $ git bisect good 598fde5

    Bisecting: 4 revisions left 

    to test after this 

    (roughly 2 steps) [6f5e527]
    $ git bisect bad

    Bisecting: 2 revisions left 

    to test after this 

    (roughly 1 step) [97b914a]
    $ git bisect good

    Bisecting: 0 revisions left 

    to test after this 

    (roughly 1 step) [40081db]
    Good

    View full-size slide

  21. Bad
    598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    $ git bisect start
    $ git bisect bad
    $ git bisect good 598fde5

    Bisecting: 4 revisions left 

    to test after this 

    (roughly 2 steps) [6f5e527]
    $ git bisect bad

    Bisecting: 2 revisions left 

    to test after this 

    (roughly 1 step) [97b914a]
    $ git bisect good

    Bisecting: 0 revisions left 

    to test after this 

    (roughly 1 step) [40081db]
    $ git bisect good

    Good

    View full-size slide

  22. Bad
    598fde5
    62d017b
    97b914a
    13f01c7
    40081db
    6f5e527
    eb2c86b
    f89d7b3
    579acfa
    b054ee3
    7aba54b
    $ git bisect start
    $ git bisect bad
    $ git bisect good 598fde5

    Bisecting: 4 revisions left 

    to test after this 

    (roughly 2 steps) [6f5e527]
    $ git bisect bad

    Bisecting: 2 revisions left 

    to test after this 

    (roughly 1 step) [97b914a]
    $ git bisect good

    Bisecting: 0 revisions left 

    to test after this 

    (roughly 1 step) [40081db]
    $ git bisect good

    6f5e527 is the first bad commit

    commit 6f5e527

    Author: Luke Stringer

    Date: Sat Mar 31


    Add user config
    Good
    First Bad
    commit

    View full-size slide

  23. © AND Digital 2023
    For Best Results
    • Have a clear de
    fi
    nition of good & bad
    • You can supply a script to git bisect to
    check if criteria is met
    • Keep commits small (enough) to inspect
    • Write descriptive commit messages to aid
    understanding
    © AND Digital 2023

    View full-size slide

  24. © AND Digital 2023
    🐛
    Luke Stringer, Tech Principal @ AND Digital
    Debugging with Git

    View full-size slide