Slide 1

Slide 1 text

Git Beyond the Basics Foivos Zakkak [email protected] Except where otherwise noted, this presentation is licensed under the Creative Commons Attribution 4.0 International License. Third party marks and brands are the property of their respective holders.

Slide 2

Slide 2 text

Preamble Feel free to interrupt if you don’t understand something Please ask. There are no stupid questions! The slides are available at https://speakerdeck.com/zakkak/git-beyond-the-basics Apologies in advance if I inadvertently offend you in any way Git: Beyond the Basics [email protected]

Slide 3

Slide 3 text

Assumptions You know how to: create a new repository git init clone git clone pull git pull commit git add ; git commit push git push use branches git branch mybranch; git checkout mybranch Git: Beyond the Basics [email protected]

Slide 4

Slide 4 text

What Is This Tutorial About? Time traveling visiting older states, undoing mistakes “Blaming” others detecting commits that introduced bugs Gardening working with multiple branches Cleaning after yourself rebasing, squash, fixup, editing logs Sharing working with multiple remotes Interacting with people working with others Befriending giants working with huge projects Best practices in general! Git: Beyond the Basics [email protected]

Slide 5

Slide 5 text

Git Terminology & Background Visualization inspired by Patrick Zahnd’s “git data transport commands”. (http://www.patrickzahnd.ch) workspace index/ staging area local repo remote repo stash git add/rm/mv git commit git push git fetch git pull git stash pop/apply git stash git checkout -- ... git reset --soft git checkout HEAD Git: Beyond the Basics [email protected]

Slide 6

Slide 6 text

Git Terminology & Background Visualization inspired by Patrick Zahnd’s “git data transport commands”. (http://www.patrickzahnd.ch) workspace index/ staging area local repo remote repo stash git add/rm/mv git commit git push git fetch git pull git stash pop/apply git stash git checkout -- ... git reset --soft git checkout HEAD Git: Beyond the Basics [email protected]

Slide 7

Slide 7 text

Git Terminology & Background Visualization inspired by Patrick Zahnd’s “git data transport commands”. (http://www.patrickzahnd.ch) workspace index/ staging area local repo remote repo stash git add/rm/mv git commit git push git fetch git pull git stash pop/apply git stash git checkout -- ... git reset --soft git checkout HEAD Git: Beyond the Basics [email protected]

Slide 8

Slide 8 text

Git Terminology & Background Visualization inspired by Patrick Zahnd’s “git data transport commands”. (http://www.patrickzahnd.ch) workspace index/ staging area local repo remote repo stash git add/rm/mv git commit git push git fetch git pull git stash pop/apply git stash git checkout -- ... git reset --soft git checkout HEAD Git: Beyond the Basics [email protected]

Slide 9

Slide 9 text

Git Terminology & Background Visualization inspired by Patrick Zahnd’s “git data transport commands”. (http://www.patrickzahnd.ch) workspace index/ staging area local repo remote repo stash git add/rm/mv git commit git push git fetch git pull git stash pop/apply git stash git checkout -- ... git reset --soft git checkout HEAD Git: Beyond the Basics [email protected]

Slide 10

Slide 10 text

Git Log Visualization beef00 beef01 beef02 beef03 beef04 beef05 beef06 beef07 beef11 beef08 beef09 beef10 feature1 feature2 feature3 master HEAD Git: Beyond the Basics [email protected]

Slide 11

Slide 11 text

Table of Contents 1 Time Travel 2 Blaming Others 3 Gardening 4 Cleaning After Yourself 5 Sharing 6 Interacting with People 7 Befriending Giants Git: Beyond the Basics [email protected]

Slide 12

Slide 12 text

Jump to a Certain Point in Time (Commit) git checkout Brings the state of in the workspace Keeps any changes if there are no conflicts, fails otherwise Resets index Might detach HEAD Git: Beyond the Basics [email protected]

Slide 13

Slide 13 text

Undo Changes in Specific File(s) git checkout -- Get (overwrite) from index If path has been staged it will revert to the staged state Git: Beyond the Basics [email protected]

Slide 14

Slide 14 text

Obtain Specific Version of Specific File(s) git checkout -- Get (overwrite) from Git: Beyond the Basics [email protected]

Slide 15

Slide 15 text

Undo Staging git reset Resets the staging area (not altering the files in the workspace) Git: Beyond the Basics [email protected]

Slide 16

Slide 16 text

Undo Staging git reset Resets the staging area (not altering the files in the workspace) git reset -- Only affect Git: Beyond the Basics [email protected]

Slide 17

Slide 17 text

Move HEAD of Current Branch to Another Branch/Commit git reset Point HEAD to --mixed (the default) change the HEAD and reset staging area Git: Beyond the Basics [email protected]

Slide 18

Slide 18 text

Move HEAD of Current Branch to Another Branch/Commit git reset Point HEAD to --mixed (the default) change the HEAD and reset staging area --soft only change the HEAD (Undo last commits without losing changes) Git: Beyond the Basics [email protected]

Slide 19

Slide 19 text

Move HEAD of Current Branch to Another Branch/Commit git reset Point HEAD to --mixed (the default) change the HEAD and reset staging area --soft only change the HEAD (Undo last commits without losing changes) --hard change the HEAD, reset staging area and workspace (Throw away last commits) Git: Beyond the Basics [email protected]

Slide 20

Slide 20 text

Move HEAD of Current Branch to Another Branch/Commit git reset Point HEAD to --mixed (the default) change the HEAD and reset staging area --soft only change the HEAD (Undo last commits without losing changes) --hard change the HEAD, reset staging area and workspace (Throw away last commits) --merge See man git reset Git: Beyond the Basics [email protected]

Slide 21

Slide 21 text

Move HEAD of Current Branch to Another Branch/Commit git reset Point HEAD to --mixed (the default) change the HEAD and reset staging area --soft only change the HEAD (Undo last commits without losing changes) --hard change the HEAD, reset staging area and workspace (Throw away last commits) --merge See man git reset --keep See man git reset Git: Beyond the Basics [email protected]

Slide 22

Slide 22 text

Undo Changes git revert Creates a new commit that reverts the changes made by Git: Beyond the Basics [email protected]

Slide 23

Slide 23 text

Undo Changes git revert Creates a new commit that reverts the changes made by git revert .. Creates a new commit that reverts the changes made by all commits in the range (inclusive) to Git: Beyond the Basics [email protected]

Slide 24

Slide 24 text

Undo Changes git revert Creates a new commit that reverts the changes made by git revert .. Creates a new commit that reverts the changes made by all commits in the range (inclusive) to git revert -n ... Reverts the changes but does not commit them Git: Beyond the Basics [email protected]

Slide 25

Slide 25 text

Table of Contents 1 Time Travel 2 Blaming Others 3 Gardening 4 Cleaning After Yourself 5 Sharing 6 Interacting with People 7 Befriending Giants Git: Beyond the Basics [email protected]

Slide 26

Slide 26 text

Blame git blame Annotates each line with the last commit that altered it Git: Beyond the Basics [email protected]

Slide 27

Slide 27 text

Blame git blame Annotates each line with the last commit that altered it git blame -w Ignores whitespace edits Git: Beyond the Basics [email protected]

Slide 28

Slide 28 text

Debugging or Bisecting git bisect start Enter the bisecting mode Git: Beyond the Basics [email protected]

Slide 29

Slide 29 text

Debugging or Bisecting git bisect start Enter the bisecting mode git bisect good/bad Mark current commit as good or bad Git: Beyond the Basics [email protected]

Slide 30

Slide 30 text

Debugging or Bisecting git bisect start Enter the bisecting mode git bisect good/bad Mark current commit as good or bad git bisect good/bad Mark as good or bad Git: Beyond the Basics [email protected]

Slide 31

Slide 31 text

Debugging or Bisecting git bisect start Enter the bisecting mode git bisect good/bad Mark current commit as good or bad git bisect good/bad Mark as good or bad git bisect log Show tested commits and their status. This log can be saved and used to replay part of the process. git bisect replay Git: Beyond the Basics [email protected]

Slide 32

Slide 32 text

Table of Contents 1 Time Travel 2 Blaming Others 3 Gardening 4 Cleaning After Yourself 5 Sharing 6 Interacting with People 7 Befriending Giants Git: Beyond the Basics [email protected]

Slide 33

Slide 33 text

Branching git branch -a List all branches, local and remote-tracking Git: Beyond the Basics [email protected]

Slide 34

Slide 34 text

Branching git branch -a List all branches, local and remote-tracking git checkout -b Fastest way to create and checkout a new branch Git: Beyond the Basics [email protected]

Slide 35

Slide 35 text

Branching git branch -a List all branches, local and remote-tracking git checkout -b Fastest way to create and checkout a new branch git branch -d Delete locally Git: Beyond the Basics [email protected]

Slide 36

Slide 36 text

Branching git branch -a List all branches, local and remote-tracking git checkout -b Fastest way to create and checkout a new branch git branch -d Delete locally git branch -m or git branch -m Rename current branch or given branch Git: Beyond the Basics [email protected]

Slide 37

Slide 37 text

Branching git branch -a List all branches, local and remote-tracking git checkout -b Fastest way to create and checkout a new branch git branch -d Delete locally git branch -m or git branch -m Rename current branch or given branch git branch -u Set the remote branch to be used as upstream for current branch Git: Beyond the Basics [email protected]

Slide 38

Slide 38 text

Merging git merge Merge with current branch --ff (the default) if merging can be resolved as an append doesn’t create a merge commit Git: Beyond the Basics [email protected]

Slide 39

Slide 39 text

Merging git merge Merge with current branch --ff (the default) if merging can be resolved as an append doesn’t create a merge commit --ff-only if merging cannot be resolved as an append it fails Git: Beyond the Basics [email protected]

Slide 40

Slide 40 text

Merging git merge Merge with current branch --ff (the default) if merging can be resolved as an append doesn’t create a merge commit --ff-only if merging cannot be resolved as an append it fails --no-ff merge and create a merge commit Git: Beyond the Basics [email protected]

Slide 41

Slide 41 text

Merging git merge Merge with current branch --ff (the default) if merging can be resolved as an append doesn’t create a merge commit --ff-only if merging cannot be resolved as an append it fails --no-ff merge and create a merge commit --squash squash all changes and stage them but do not commit Git: Beyond the Basics [email protected]

Slide 42

Slide 42 text

Rebasing git rebase Replay current branch’s commits on top of Caution Re-writes the history of your branch, changing the commit hashes Git: Beyond the Basics [email protected]

Slide 43

Slide 43 text

Table of Contents 1 Time Travel 2 Blaming Others 3 Gardening 4 Cleaning After Yourself 5 Sharing 6 Interacting with People 7 Befriending Giants Git: Beyond the Basics [email protected]

Slide 44

Slide 44 text

Re-writing History git rebase -i Opens a file with all commits from HEAD to (inclusive) and allows us to: drop reorder edit commit squash fixup edit commit message Caution Re-writes the history of your branch, changing the commit hashes Git: Beyond the Basics [email protected]

Slide 45

Slide 45 text

Fixup and Squash Commits git commit --fixup/--squash Mark commit as a fixup or squash of . Commit message will automatically be set. Git: Beyond the Basics [email protected]

Slide 46

Slide 46 text

Fixup and Squash Commits git commit --fixup/--squash Mark commit as a fixup or squash of . Commit message will automatically be set. git rebase -i --autosquash Automatically squash commits starting with fixup! or squash! Git: Beyond the Basics [email protected]

Slide 47

Slide 47 text

Table of Contents 1 Time Travel 2 Blaming Others 3 Gardening 4 Cleaning After Yourself 5 Sharing 6 Interacting with People 7 Befriending Giants Git: Beyond the Basics [email protected]

Slide 48

Slide 48 text

Managing Multiple Remotes git remote add Adds a new remote Git: Beyond the Basics [email protected]

Slide 49

Slide 49 text

Managing Multiple Remotes git remote add Adds a new remote git push/pull/fetch Pushes/Pulls/Fetches to instead of origin Git: Beyond the Basics [email protected]

Slide 50

Slide 50 text

Managing Multiple Remotes git remote add Adds a new remote git push/pull/fetch Pushes/Pulls/Fetches to instead of origin git checkout/merge / Checks out/Merges from instead of the local . It still doesn’t fetch it though! Git: Beyond the Basics [email protected]

Slide 51

Slide 51 text

Managing Multiple Remotes git fetch --all Fetches all remotes Git: Beyond the Basics [email protected]

Slide 52

Slide 52 text

Managing Multiple Remotes git fetch --all Fetches all remotes git fetch --all --prune Removes any no longer existing remote branches Git: Beyond the Basics [email protected]

Slide 53

Slide 53 text

Table of Contents 1 Time Travel 2 Blaming Others 3 Gardening 4 Cleaning After Yourself 5 Sharing 6 Interacting with People 7 Befriending Giants Git: Beyond the Basics [email protected]

Slide 54

Slide 54 text

Rules for Successful Collaboration Use descriptive yet short commit subjects Git: Beyond the Basics [email protected]

Slide 55

Slide 55 text

Rules for Successful Collaboration Use descriptive yet short commit subjects Never rewrite history of shared branches Git: Beyond the Basics [email protected]

Slide 56

Slide 56 text

Rules for Successful Collaboration Use descriptive yet short commit subjects Never rewrite history of shared branches Merge often Git: Beyond the Basics [email protected]

Slide 57

Slide 57 text

Rules for Successful Collaboration Use descriptive yet short commit subjects Never rewrite history of shared branches Merge often Do not commit changes that break the previous state Git: Beyond the Basics [email protected]

Slide 58

Slide 58 text

Rules for Successful Collaboration Use descriptive yet short commit subjects Never rewrite history of shared branches Merge often Do not commit changes that break the previous state Keep commits self contained and as small as possible Git: Beyond the Basics [email protected]

Slide 59

Slide 59 text

Bonus git add -p Interactive staging allowing to only stage parts of a diff (hunks) Git: Beyond the Basics [email protected]

Slide 60

Slide 60 text

Bonus git add -p Interactive staging allowing to only stage parts of a diff (hunks) git cherry-pick Cherry-pick a single commit from another branch Git: Beyond the Basics [email protected]

Slide 61

Slide 61 text

Table of Contents 1 Time Travel 2 Blaming Others 3 Gardening 4 Cleaning After Yourself 5 Sharing 6 Interacting with People 7 Befriending Giants Git: Beyond the Basics [email protected]

Slide 62

Slide 62 text

Working with Huge Projects Consider the gating/regression costs before pushing Git: Beyond the Basics [email protected]

Slide 63

Slide 63 text

Working with Huge Projects Consider the gating/regression costs before pushing “I’ll fix it later” hacks are not going to be fixed any time soon Oldest “fix later” linux kernel hack dates back to 1996 http://kazet.co/2016/04/29/temporary-hacks.html Git: Beyond the Basics [email protected]

Slide 64

Slide 64 text

Working with Huge Projects Consider the gating/regression costs before pushing “I’ll fix it later” hacks are not going to be fixed any time soon Oldest “fix later” linux kernel hack dates back to 1996 http://kazet.co/2016/04/29/temporary-hacks.html Follow a strict branching model like Git-Flow Git: Beyond the Basics [email protected]

Slide 65

Slide 65 text

Working with Huge Projects Consider the gating/regression costs before pushing “I’ll fix it later” hacks are not going to be fixed any time soon Oldest “fix later” linux kernel hack dates back to 1996 http://kazet.co/2016/04/29/temporary-hacks.html Follow a strict branching model like Git-Flow git log --simplify-merges Git: Beyond the Basics [email protected]

Slide 66

Slide 66 text

Indicative Workflow 1. Code Git: Beyond the Basics [email protected]

Slide 67

Slide 67 text

Indicative Workflow 1. Code 2. Commit Git: Beyond the Basics [email protected]

Slide 68

Slide 68 text

Indicative Workflow 1. Code 2. Commit 3. Gate (pre-push tests) Git: Beyond the Basics [email protected]

Slide 69

Slide 69 text

Indicative Workflow 1. Code 2. Commit 3. Gate (pre-push tests) 4. Push Git: Beyond the Basics [email protected]

Slide 70

Slide 70 text

Indicative Workflow 1. Code 2. Commit 3. Gate (pre-push tests) 4. Push 5. Open Pull/Merge Request Git: Beyond the Basics [email protected]

Slide 71

Slide 71 text

Indicative Workflow 1. Code 2. Commit 3. Gate (pre-push tests) 4. Push 5. Open Pull/Merge Request 6. Code Review and Testing Git: Beyond the Basics [email protected]

Slide 72

Slide 72 text

Indicative Workflow 1. Code 2. Commit 3. Gate (pre-push tests) 4. Push 5. Open Pull/Merge Request 6. Code Review and Testing 7. Merge Git: Beyond the Basics [email protected]

Slide 73

Slide 73 text

Indicative Workflow 1. Code 2. Commit 3. Gate (pre-push tests) 4. Push 5. Open Pull/Merge Request 6. Code Review and Testing 7. Merge 8. Regression Testing Git: Beyond the Basics [email protected]

Slide 74

Slide 74 text

Cheers! Git Beyond the Basics Foivos Zakkak [email protected]