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 Slide

  2. © AND Digital 2023

    View 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 Slide

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

    View Slide

  5. © AND Digital 2023
    Use your Git history

    View Slide

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

    View Slide

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

    View 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 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 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 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 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 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 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 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 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 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 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 Slide

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

    View Slide