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

Git: Beyond the Basics

zakkak
January 16, 2018

Git: Beyond the Basics

zakkak

January 16, 2018
Tweet

More Decks by zakkak

Other Decks in Programming

Transcript

  1. 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.
  2. 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]
  3. Assumptions You know how to: create a new repository git

    init clone git clone <path.to.git.repository> pull git pull commit git add <file>; git commit push git push use branches git branch mybranch; git checkout mybranch Git: Beyond the Basics [email protected]
  4. 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]
  5. 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]
  6. 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]
  7. 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]
  8. 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]
  9. 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]
  10. 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]
  11. 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]
  12. Jump to a Certain Point in Time (Commit) git checkout

    <branch/commit> Brings the state of <branch/commit> in the workspace Keeps any changes if there are no conflicts, fails otherwise Resets index Might detach HEAD Git: Beyond the Basics [email protected]
  13. Undo Changes in Specific File(s) git checkout -- <path(s)> Get

    (overwrite) <path(s)> from index If path has been staged it will revert to the staged state Git: Beyond the Basics [email protected]
  14. Obtain Specific Version of Specific File(s) git checkout <branch/commit> --

    <path(s)> Get (overwrite) <path(s)> from <branch/commit> Git: Beyond the Basics [email protected]
  15. Undo Staging git reset Resets the staging area (not altering

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

    the files in the workspace) git reset -- <path(s)> Only affect <path(s)> Git: Beyond the Basics [email protected]
  17. Move HEAD of Current Branch to Another Branch/Commit git reset

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

    <branch/commit> Point HEAD to <branch/commit> --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]
  19. Move HEAD of Current Branch to Another Branch/Commit git reset

    <branch/commit> Point HEAD to <branch/commit> --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]
  20. Move HEAD of Current Branch to Another Branch/Commit git reset

    <branch/commit> Point HEAD to <branch/commit> --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]
  21. Move HEAD of Current Branch to Another Branch/Commit git reset

    <branch/commit> Point HEAD to <branch/commit> --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]
  22. Undo Changes git revert <commit> Creates a new commit that

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

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

    reverts the changes made by <commit> git revert <commit1>..<commit2> Creates a new commit that reverts the changes made by all commits in the range (inclusive) <commit1> to <commit2> git revert -n ... Reverts the changes but does not commit them Git: Beyond the Basics [email protected]
  25. 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]
  26. 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]
  27. 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]
  28. 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 <commit> Mark <commit> as good or bad Git: Beyond the Basics [email protected]
  29. 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 <commit> Mark <commit> 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 <logfile> Git: Beyond the Basics [email protected]
  30. 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]
  31. Branching git branch -a List all branches, local and remote-tracking

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

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

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

    git checkout -b <newbranch> Fastest way to create and checkout a new branch git branch -d <branchname> Delete <branchname> locally git branch -m <newname> or git branch -m <oldname> <newname> Rename current branch or given branch git branch -u <remote> <remotebranchname> Set the remote branch to be used as upstream for current branch Git: Beyond the Basics [email protected]
  35. Merging git merge <branch> Merge <branch> 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]
  36. Merging git merge <branch> Merge <branch> 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]
  37. Merging git merge <branch> Merge <branch> 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]
  38. Merging git merge <branch> Merge <branch> 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]
  39. Rebasing git rebase <branch> Replay current branch’s commits on top

    of <branch> Caution Re-writes the history of your branch, changing the commit hashes Git: Beyond the Basics [email protected]
  40. 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]
  41. Re-writing History git rebase -i <commit> Opens a file with

    all commits from HEAD to <commit> (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]
  42. Fixup and Squash Commits git commit --fixup/--squash <commit> Mark commit

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

    as a fixup or squash of <commit>. Commit message will automatically be set. git rebase -i --autosquash Automatically squash commits starting with fixup! or squash! Git: Beyond the Basics [email protected]
  44. 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]
  45. Managing Multiple Remotes git remote add <remotename> <url> Adds a

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

    new remote git push/pull/fetch <remotename> Pushes/Pulls/Fetches to <remotename> instead of origin git checkout/merge <remotename>/<branch> Checks out/Merges <branch> from <remotename> instead of the local <branch>. It still doesn’t fetch it though! Git: Beyond the Basics [email protected]
  47. 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]
  48. 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]
  49. Rules for Successful Collaboration Use descriptive yet short commit subjects

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

    Never rewrite history of shared branches Merge often Git: Beyond the Basics [email protected]
  51. 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]
  52. 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]
  53. 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]
  54. 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]
  55. 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]
  56. 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]
  57. 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]
  58. Indicative Workflow 1. Code 2. Commit 3. Gate (pre-push tests)

    4. Push 5. Open Pull/Merge Request Git: Beyond the Basics [email protected]
  59. 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]
  60. 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]
  61. 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]