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

Story-telling with Git rebase

Elle Meredith
November 30, 2018

Story-telling with Git rebase

In a successful software development project, a key challenge is to manage complexity because projects get very complex very quickly even within small teams. Version control is the tool for communicating intent in our codebase over the life time of the project. Rebasing allows us to revise our development history before sharing it with the team. Learn to use Git commit messages to keep track of the intent of your code changes to make it easier to make future changes. Learn how to make using feature branches less painful and more effective. Learn the mechanics of interactive rebasing, how to merge conflicts without losing precious code and how to auto-squash commits. Basically, stop fearing interactive rebasing.

Elle Meredith

November 30, 2018
Tweet

More Decks by Elle Meredith

Other Decks in Programming

Transcript

  1. STORY-TELLING WITH
    GIT REBASE
    Elle Meredith @aemeredith
    Blackmill @blackmillco

    View full-size slide

  2. Version Control System

    View full-size slide

  3. A project’s history is its most
    valuable documentation
    Ref: https://mislav.net/2014/02/hidden-documentation/

    View full-size slide

  4. Kept forever

    View full-size slide

  5. Always in sync with the code

    View full-size slide

  6. Are available to everyone

    View full-size slide

  7. $ git log --grep regexp

    View full-size slide

  8. SMALL COMMITS
    1

    View full-size slide

  9. commit [REDACTED]
    Date: [REDACTED]
    - add promotions to custom brand bundles
    - add bundle promotions to custom brands
    - generate unique custom promo code key
    - remove validation for nested create
    - remove binding.pry
    - update to allow default bundle (no promo)
    - refactor for empty promotion
    - tmp precompile assets
    - refactor dynamic promo code
    - adding tracking, top/bottom code
    - removing unused method calls, redundant migration
    - use scope
    - style with Rubocop validation
    - update asset path, fix broken specs
    - tmp disable integration spec
    20 files changed, 349 insertions(+), 129 deletions(-)

    View full-size slide

  10. How big or small should a
    commit be?

    View full-size slide

  11. $ git add -p

    View full-size slide

  12. Image credit: http://www.artslife.com/2014/09/22/people-places-steve-mccurry-alla-south-
    bank-tower- no-al-279/
    GOOD COMMIT MESSAGES
    2

    View full-size slide

  13. Ref: http://stevetarver.github.io/2016/02/19/intentional-git-comments.html
    Image credit: https://burst.shopify.com/photos/train-turning-through-fog
    1. Clarity of intent
    2. Clarity of thought
    3. Ability to reason
    about the code

    View full-size slide

  14. Ref: https://github.com/kristinabrown/dinner-dash/commits/master?page=5

    View full-size slide

  15. $ Fix failed specs

    View full-size slide

  16. $ Changed stuff

    View full-size slide

  17. $ Testing git push

    View full-size slide

  18. $ Adjust css

    View full-size slide

  19. $ Minor changes

    View full-size slide

  20. $ Work on feature XYZ

    View full-size slide

  21. $ Change X constant to be 10

    View full-size slide

  22. $ Should work now

    View full-size slide

  23. $ WTF?
    Ref: https://www.codelord.net/2015/03/16/bad-commit-messages-hall-of-shame/

    View full-size slide

  24. Image credit: https://weheartit.com/entry/104254659

    View full-size slide

  25. ANSWER 5 QUESTIONS
    Why is this change necessary?
    How does it address the issue?
    What side e ects does this change have?
    Were other solutions considered?
    Include a reference [to a discussion, resource, ticket ]
    Ref: https://robots.thoughtbot.com/5-useful-tips-for-a-better-commit-message

    View full-size slide

  26. Present-tense summary under 50 characters
    * More information about commit (under 72 chars).
    * More information about commit (under 72 chars).
    http://project.management-system.com/ticket/123

    View full-size slide

  27. Present-tense summary under 50 characters
    * More information about commit (under 72 chars).
    * More information about commit (under 72 chars).
    http://project.management-system.com/ticket/123

    View full-size slide

  28. Present-tense summary under 50 characters
    * More information about commit (under 72 chars).
    * More information about commit (under 72 chars).
    http://project.management-system.com/ticket/123

    View full-size slide

  29. commit [REDACTED]
    Author: Elle Meredith
    Date: [REDACTED]
    Add author's name to opportunity form
    - This input field is disabled
    - so that the user cannot edit the field
    - It displays the author's name on:
    - create a new opportunity
    - edit an existing opportunity
    - reposting an opportunity
    - If the author doesn't have a first or last name,
    display the author's email instead
    Trello: https://trello.com/c/12345

    View full-size slide

  30. 1. Add (340 times)
    2. Update (339 times)
    3. Remove / Delete (135 times)
    4. Fix (132 times)
    5. Use (76 times)
    6. Style (56 times)
    7. Allow (53 times)
    8. Bump (50 times)
    9. Show (44 times)
    10. Refactor / Clean (35 times)
    Apply / Change / Con gure / Create / Deprecate /
    Disable / Display / Drop / Enable / Ensure / Extract
    / Handle / Improve / Increase / Limit / Link /
    Make / Migrate / Rename / Set / Skip / Sort /
    Support / Validate / Wire

    View full-size slide

  31. SMALL FEATURE BRANCHES
    3

    View full-size slide

  32. Encapsulation

    View full-size slide

  33. Pull requests
    and code reviews

    View full-size slide

  34. 1. Create a new branch
    $ git checkout -b em-feature-branch

    View full-size slide

  35. 2. Update the branch, commit new changes
    $ git status
    $ git add
    $ git commit

    View full-size slide

  36. 3. Push changes in the branch to `origin`
    $ git push -u origin em-feature-branch
    or
    $ git push origin head

    View full-size slide

  37. 4. Open a PR for feedback

    View full-size slide

  38. 5. Implement changes based on feedback

    View full-size slide

  39. 6. Rebase master on the branch interactively
    and push updated branch back up
    $ git rebase -i master
    $ git push origin head -f

    View full-size slide

  40. 6. Rebase master on the branch interactively
    and push updated branch back up
    $ git rebase -i master
    $ git push origin head -f

    View full-size slide

  41. 7. Then fast forward merge to master
    $ git checkout master
    $ git merge --ff-only em-feature-branch

    View full-size slide

  42. 8. Lastly clean up the branches
    $ git push origin :em-feature-branch
    $ git branch -d em-feature-branch
    Ref: https://github.com/thoughtbot/guides/tree/master/protocol/git

    View full-size slide

  43. MERGING VS
    REBASING

    View full-size slide

  44. $ git merge --no-ff em-feature-branch

    View full-size slide

  45. Image credit: https://www.atlassian.com/git/tutorials/merging-vs-rebasing

    View full-size slide

  46. $ git rebase -i origin/master

    View full-size slide

  47. $ git rebase --continue
    $ git rebase --abort

    View full-size slide

  48. Rebase 34020519..5eac86e0 onto 34020519 (2 commands)
    Commands:
    p, pick = use commit
    r, reword = use commit, but edit the commit message
    e, edit = use commit, but stop for amending
    s, squash = use commit, but meld into previous commit
    f, fixup = like "squash", but discard this commit's log message
    x, exec = run command (the rest of the line) using shell
    d, drop = remove commit
    l, label = label current HEAD with a name
    t, reset = reset HEAD to a label
    m, merge [-C | -c ] [# ]
    create a merge commit using the original merge commit's
    message (or the oneline, if no original merge commit was
    specified). Use -c to reword the commit message.
    These lines can be re-ordered;\they are executed from top to bottom.
    If you remove a line here THAT COMMIT WILL BE LOST.
    However, if you remove everything, the rebase will be aborted.
    Note that empty commits are commented out

    View full-size slide

  49. Auto-squash and xup
    Ref: https://robots.thoughtbot.com/autosquashing-git-commits

    View full-size slide

  50. $ git commit --fixup
    $ git rebase --interactive --autosquash master

    View full-size slide

  51. $ git config --global rebase.autosquash true

    View full-size slide

  52. Rebase often to grab recent updates from `master`
    Use rebase when you want to keep a linear commit
    history
    Never use rebase on a public/shared branch- i.e. never
    rebase `master`

    View full-size slide

  53. WASH, RINSE, AND REPEAT

    View full-size slide

  54. Make small feature-
    speci c commits

    View full-size slide

  55. Write good commit
    messages

    View full-size slide

  56. Work in small thematic
    feature branches

    View full-size slide

  57. Rebase frequently to
    incorporate upstream
    changes

    View full-size slide

  58. Review and revise
    history before sharing

    View full-size slide

  59. Keep history linear

    View full-size slide

  60. Auto-squash commits

    View full-size slide

  61. THANK YOU! Elle Meredith @aemeredith
    Blackmill @blackmillco
    Edward watching
    over my sourdough

    View full-size slide