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

Git Branching for Success and Profit

Lorna Mitchell
December 19, 2014

Git Branching for Success and Profit

Daycamp for developers, this slide deck accompanied my session at the excellent virtual event that is DC4D.

Lorna Mitchell

December 19, 2014
Tweet

More Decks by Lorna Mitchell

Other Decks in Technology

Transcript

  1. Git Branching For
    Success and Profit
    Lorna Mitchell, Day Camp For
    Developers

    View Slide

  2. Git Branching

    View Slide

  3. A Branch is a Label
    Take a look in .git/refs/heads/
    • A branch is a pointer to a commit.
    • You commit on a branch, the commit is
    created and the pointer updated.

    View Slide

  4. Commits Aren't "On" Branches

    View Slide

  5. Commits Aren't "On" Branches

    View Slide

  6. 11% of branches never
    get merged

    View Slide

  7. 86% of statistics are
    made up on the spot

    View Slide

  8. Git Branch
    We are committing along:

    View Slide

  9. Git Branch
    We create a branch and keep committing:
    git branch feature and git checkout feature
    git checkout -b feature

    View Slide

  10. Git Branch
    Stuff happens on master meanwhile:

    View Slide

  11. Branching is easy.
    Merging is ?

    View Slide

  12. Branching is easy.
    Merging is easy when
    you know how.

    View Slide

  13. Git Merge

    View Slide

  14. Git Merge
    The plan:
    • Check out the branch that changes should
    arrive into.
    • Merge.
    git checkout master
    git merge feature

    View Slide

  15. Git Merge

    View Slide

  16. Git Undo

    View Slide

  17. Git Undo
    How to undo a merge:
    • Set the branch pointer back to where it
    was before the merge
    git reset --hard 7e386
    (your branch is still intact)

    View Slide

  18. Git Undo

    View Slide

  19. Undo an Old Merge
    Use git revert but specify which way you want
    to go at the fork
    git revert -m 1 d1151

    View Slide

  20. Merge Conflicts
    Merge conflicts occur when git needs some
    help.

    View Slide

  21. DON'T PANIC

    View Slide

  22. Merge Conflicts: Your Options
    • git merge --abort
    • Edit the files until things are correct
    • Use git checkout --[ours|theirs] to get the
    right version in place
    Fix the conflict, then add and commit the file(s).
    Your merge will complete

    View Slide

  23. Merge Types

    View Slide

  24. Fast-Forward Merge
    No changes on master since we branched

    View Slide

  25. Fast-Forward Merge
    git merge feature

    View Slide

  26. Force Recursive Merge
    Start from the same place:

    View Slide

  27. Force Recursive Merge
    Then merge without fast-forward:
    git merge --no-ff feature

    View Slide

  28. Git Rebase

    View Slide

  29. Git Rebase
    Rebase to move a branch start point

    View Slide

  30. Git Rebase
    git rebase master
    We can now fast-forward merge

    View Slide

  31. Rebase Conflicts
    Rebase applies each of your commits in turn.
    This can result in a conflict at any point.
    Handle exactly as you would for merge
    conflicts. Then type git rebase --continue.
    git rebase --abort is always an option.

    View Slide

  32. Interactive Rebase
    Rewrite the last N commits. Your options:
    • remove a commit completely
    • re-order commits
    • combine commits and/or re-word their
    commit messages

    View Slide

  33. Branching Strategies

    View Slide

  34. Branching Strategies
    A branching strategy is basically a git process
    document. It covers:
    • when to branch
    • where to branch from
    • what to call your branch
    • where to merge to

    View Slide

  35. Git Flow
    Comprehensive branching strategy, supported
    by scripts.
    • Article: http://lrnja.net/1ebawKU
    • Scripts: https://github.com/nvie/gitflow

    View Slide

  36. Git Flow

    View Slide

  37. Git Flow

    View Slide

  38. Git Flow

    View Slide

  39. Git Flow

    View Slide

  40. Git Flow

    View Slide

  41. Other Branching Strategies
    • GitHub Flow: http://lrnja.net/1Gr29Jn
    • Atlassian blog: http://lrnja.net/1BSFQcQ
    • Docs from GitLab: http://lrnja.net/1AiPxQJ
    Some common elements can be spotted
    throughout

    View Slide

  42. Branching Strategy
    Ingredients List

    View Slide

  43. Topic Branches Pattern
    Starting something new? Use a branch.

    View Slide

  44. Topic Branches Pattern
    Merge from master into feature before merging

    View Slide

  45. Topic Branches Pattern
    Rebase branch onto master before merging

    View Slide

  46. Master vs Develop
    This one is a matter of taste!

    View Slide

  47. Hotfix Pattern
    Branch off, commit once, merge with --no-ff
    • makes it very clear what happened
    • easily merge the branch back upstream if
    needed
    • makes it possible to cleanly undo without
    losing anything

    View Slide

  48. Hotfix Pattern

    View Slide

  49. Environment Branches
    Maintain one branch per platform
    • branch always reflects current state of
    platform
    • plays well with automated deployment

    View Slide

  50. Environment Branches

    View Slide

  51. Tagging for Releases
    Git has two types of tags:
    • simple tags are just a label, like a branch that
    doesn't advance
    • annotated tags are an object, they can
    contain messages
    git tag -a v2.11 -m "v2.11 with caching"
    Then push the tag

    View Slide

  52. Git Branches

    View Slide

  53. Feedback (and Resources)
    • How was it? https://m.joind.in/talk/50bd3
    • Links bundle: http://lrnja.net/135jCIy
    • My git book: http://gitworkbook.com/
    See Also:
    • Matthew's Scripts: http://lrnja.net/1AiQrgh

    View Slide