Effective Git

Effective Git

1633e2784d925ea41167b45bccddcd19?s=128

Michael Cordell

November 03, 2016
Tweet

Transcript

  1. If that doesn't fix it, git.txt contains the phone number

    of a friend of mine who understands git. Just wait through a few minutes of 'It's really pretty simple, just think of branches as...' and eventually you'll learn the commands that will fix everything.
  2. EFFECTIVE GIT Michael Cordell

  3. GIT: THE BASICS git is a distributed version control system

  4. EDITING class Startup def increase_roi step_one step_two “????” profit! end

    end
  5. STAGING ƭ startup.rb git add startup.rb Index

  6. COMMITTING ƭ startup.rb Index git commit -m “Add the startup!”

  7. ƭ ƭ ƭ ƭ

  8. BRANCHING master git checkout -b feature feature

  9. BRANCHING master feature git commit

  10. BRANCHING master feature git commit

  11. BRANCHING feature git checkout master master

  12. BRANCHING feature git commit master

  13. BRANCHING feature git merge feature master

  14. WHY

  15. Code Editor || IDE VCS Build Tools

  16. Build Tools Editor/IDE Now VCS

  17. Since git deals with history, git’s effectiveness as a tool

    is directly related to how it has been used in the past
  18. GUIDING PRINCIPLES

  19. Short, focused commits

  20. None
  21. None
  22. Short, focused commits

  23. Meaningful commit messages

  24. KEY GIT PROPERTIES

  25. GIT HISTORY IS A GRAPH

  26. git history is a directed acyclic graph

  27. git history is a directed acyclic graph

  28. git history is a directed acyclic graph X

  29. Commits in git are immutable

  30. Commits in git are immutable Attempting to change: • Message

    • Content (file objects) • Parent commit results in the creation of a new commit
  31. In contrast, references in git are cheap and movable

  32. IN PRACTICE

  33. IN PRACTICE: CONFIGURATION Learn to use aliases for faster git

    commands In your shell config: In your ~/.gitconfig $ git status $ g s
  34. None
  35. None
  36. IN PRACTICE: STAGING git add -p not git add .

    Use editor plugins for easy staging by hunk or line
  37. None
  38. None
  39. IN PRACTICE: COMMITTING First line is capitalized, short (50 chars

    or less) summary Second line is blank Rest of commit message should explain why and other useful information about the change See Tim Pope’s blog post on commit messages
  40. RE-WRITING HISTORY

  41. AMENDING git commit --amend alias cam = git commit --amend

    --no-edit
  42. REBASING master feature git merge feature

  43. REBASING master git reset --hard HEAD~1 feature

  44. REBASING master feature git checkout feature

  45. REBASING master feature git rebase master

  46. REBASING master git rebase master

  47. REBASING git rebase master master

  48. REBASING git rebase master master feature

  49. master feature master feature f87cb a98fc cc47a 18dd2 744ee 8ca01

    8ca01 ac871 ac871
  50. master f87cb a98fc cc47a 18dd2 744ee 8ca01 feature ac871 git

    checkout feature
  51. f87cb cc47a 18dd2 8ca01 ac871 master a98fc 744ee feature git

    reset --hard f87cb
  52. INTERACTIVE REBASE git rebase -i master

  53. None
  54. rebase conflicts are the same as merge conflicts, resolve and

    git rebase -i master
  55. git diff against the pre-rebase commit if you are worried

    about conflict mistakes
  56. USING THE HISTORY

  57. None
  58. None
  59. IN PRACTICE: BLAMING Use git blame to find why a

    line changed -w flag to ignore whitespace changes
  60. None
  61. None
  62. None
  63. GIT BISECT FOR BUG HUNTING master

  64. GIT BISECT FOR BUG HUNTING master

  65. GIT BISECT FOR BUG HUNTING master git bisect start

  66. GIT BISECT FOR BUG HUNTING master git bisect good HEAD~7

  67. GIT BISECT FOR BUG HUNTING master git bisect bad master

  68. GIT BISECT FOR BUG HUNTING master git bisect

  69. GIT BISECT FOR BUG HUNTING master git bisect bad

  70. GIT BISECT FOR BUG HUNTING master

  71. GIT BISECT FOR BUG HUNTING master git bisect good

  72. GIT BISECT FOR BUG HUNTING master

  73. GIT BISECT FOR BUG HUNTING master git bisect bad

  74. MOVING/COMPARING CODE git checkout diff reset REF { }

  75. MOVING/COMPARING CODE git checkout diff reset REF { } switch

    to that reference
  76. MOVING/COMPARING CODE git checkout diff reset REF { } compare

    current against that reference
  77. MOVING/COMPARING CODE git checkout diff reset REF { } get

    the files at that reference
  78. MOVING/COMPARING CODE git checkout diff reset { }REF

  79. MOVING/COMPARING CODE git checkout diff reset REF -- FILES {

    }
  80. FIXING “MISTAKES”

  81. In git, nothing added is ever “lost”

  82. FIXING A BAD MERGE Usually this is as simple as

    backing the commit out by reseting one commit back git reset --hard HEAD~1 If you want to just fix a single badly merged file checkout the file you want git checkout GOOD_REF -- file
  83. REFLOG FOR EVERYTHING ELSE git reflog keeps a reference of

    reach change in the HEAD reference (checkouts, rebase, cherry-picks, etc.) find the commit from before the change you want to fix and reset/checkout against it reflog expires after 90 days, but you can turn this off and have it never expire
  84. None
  85. None
  86. None
  87. git checkout diff reset b73c8c7 { }

  88. LOST A STASH? git fsck can be used to find

    files that were at one time staged or perhaps stashed and dropped
  89. REMOTES

  90. REMOTES master

  91. REMOTES master origin/master git pull

  92. REMOTES master origin/master git pull

  93. REMOTES master origin/master git pull

  94. REMOTES master origin/master git pull

  95. If you are going to force push prefer: git push

    --force-with-lease
  96. End

  97. FURTHER READING Think Like a Git Hartl’s Free eBook Cook’s

    Git from the inside out