Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Git Branching

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

Commits Aren't "On" Branches

Slide 5

Slide 5 text

Commits Aren't "On" Branches

Slide 6

Slide 6 text

11% of branches never get merged

Slide 7

Slide 7 text

86% of statistics are made up on the spot

Slide 8

Slide 8 text

Git Branch We are committing along:

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Git Branch Stuff happens on master meanwhile:

Slide 11

Slide 11 text

Branching is easy. Merging is ?

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Git Merge

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Git Merge

Slide 16

Slide 16 text

Git Undo

Slide 17

Slide 17 text

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)

Slide 18

Slide 18 text

Git Undo

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Merge Conflicts Merge conflicts occur when git needs some help.

Slide 21

Slide 21 text

DON'T PANIC

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Merge Types

Slide 24

Slide 24 text

Fast-Forward Merge No changes on master since we branched

Slide 25

Slide 25 text

Fast-Forward Merge git merge feature

Slide 26

Slide 26 text

Force Recursive Merge Start from the same place:

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Git Rebase

Slide 29

Slide 29 text

Git Rebase Rebase to move a branch start point

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Branching Strategies

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Git Flow

Slide 37

Slide 37 text

Git Flow

Slide 38

Slide 38 text

Git Flow

Slide 39

Slide 39 text

Git Flow

Slide 40

Slide 40 text

Git Flow

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Branching Strategy Ingredients List

Slide 43

Slide 43 text

Topic Branches Pattern Starting something new? Use a branch.

Slide 44

Slide 44 text

Topic Branches Pattern Merge from master into feature before merging

Slide 45

Slide 45 text

Topic Branches Pattern Rebase branch onto master before merging

Slide 46

Slide 46 text

Master vs Develop This one is a matter of taste!

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

Hotfix Pattern

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Environment Branches

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

Git Branches

Slide 53

Slide 53 text

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