Practical Git(Hub)

Practical Git(Hub)

Git basics, best practices, tips & tricks. Talk given to my teammates at Kerad Games.

8837458bad71ad8edb2fbbd4dc9e8bb2?s=128

Xavier Jurado

February 17, 2016
Tweet

Transcript

  1. Practical Git(Hub)

  2. Git basics Best practices Rebase & you Tips & tricks

  3. ❝ git gets easier once you get the basic idea

    that branches are homeomorphic endofunctors mapping submanifolds of a Hilbert space ❞ -- The Internet
  4. ❝ SourceTree sucks ❞ -- Me

  5. Git basics

  6. Git is… …a distributed revision control and source code management

    system. … not an evolution of SVN. … not easy.
  7. add am apply archimport archive bisect blame branch bundle cat-file

    check-attr check-ignore check-mailmap check-ref- format checkout checkout- index cherry cherry-pick citool clean clone commit commit-tree config count-objects cvsexportcomm it cvsimport cvsserver daemon describe diff diff-files diff-index diff-tree difftool fast-export fast-import fetch fetch-pack filter-branch fmt-merge-msg for-each-ref format-patch fsck gc get-tar- commit-id grep gui hash-object help http-backend http-fetch http-push imap-send index-pack init instaweb log ls-files ls-remote ls-tree mailinfo mailsplit merge merge-base merge-file merge-index merge-one- file merge-tree mergetool mktag mktree mv name-rev notes pack-objects pack- redundant pack-refs parse-remote patch-id prune prune-packed pull push quiltimport read-tree rebase receive-pack reflog relink remote repack replace request-pull rerere reset rev-list rev-parse revert rm send-email send-pack shell shortlog show show-branch show-index show-ref stash status stripspace submodule svn symbolic-ref tag unpack-file unpack- objects update-index update-ref update- server-info upload- archive upload-pack var verify-pack verify-tag whatchanged write-tree
  8. add am apply archimport archive bisect blame branch bundle cat-file

    check-attr check-ignore check-mailmap check-ref- format checkout checkout- index cherry cherry-pick citool clean clone commit commit-tree config count-objects cvsexportcomm it cvsimport cvsserver daemon describe diff diff-files diff-index diff-tree difftool fast-export fast-import fetch fetch-pack filter-branch fmt-merge-msg for-each-ref format-patch fsck gc get-tar- commit-id grep gui hash-object help http-backend http-fetch http-push imap-send index-pack init instaweb log ls-files ls-remote ls-tree mailinfo mailsplit merge merge-base merge-file merge-index merge-one- file merge-tree mergetool mktag mktree mv name-rev notes pack-objects pack- redundant pack-refs parse-remote patch-id prune prune-packed pull push quiltimport read-tree rebase receive-pack reflog relink remote repack replace request-pull rerere reset rev-list rev-parse revert rm send-email send-pack shell shortlog show show-branch show-index show-ref stash status stripspace submodule svn symbolic-ref tag unpack-file unpack- objects update-index update-ref update- server-info upload- archive upload-pack var verify-pack verify-tag whatchanged write-tree
  9. Making changes …or how add, commit, checkout works HEAD Index

    Working Directory checkout add commit reset
  10. Grouping changes …or how to use branches via checkout, branch,

    merge A develop HEAD
  11. Grouping changes …or how to use branches via checkout, branch,

    merge A develop feature HEAD git branch feature
  12. Grouping changes …or how to use branches via checkout, branch,

    merge A develop feature HEAD git branch feature git checkout feature
  13. Grouping changes …or how to use branches via checkout, branch,

    merge A develop feature HEAD git branch feature git checkout feature git add & git commit B
  14. Grouping changes …or how to use branches via checkout, branch,

    merge A develop feature HEAD git branch feature git checkout feature git add & git commit git add & git commit B C
  15. Grouping changes …or how to use branches via checkout, branch,

    merge A develop feature HEAD git branch feature git checkout feature git add & git commit git add & git commit … B C D E
  16. Grouping changes …or how to use branches via checkout, branch,

    merge A develop feature HEAD git branch feature git checkout feature git add & git commit git add & git commit … git checkout develop B C D E
  17. Grouping changes …or how to use branches via checkout, branch,

    merge A develop feature HEAD git branch feature git checkout feature git add & git commit git add & git commit … git checkout develop git merge feature B C D E F
  18. Best practices Good

  19. commit commit often write good commit messages do not commit

    unrelated changes together
  20. commit commit often write good commit messages do not commit

    unrelated changes together
  21. branches anything in master is deployable anything in develop will

    be delivered in the next release * feature-branches is where we should commit
  22. branches anything in master is deployable anything in develop will

    be delivered in the next release feature-branches is where we should commit
  23. branches It’s ok to have different branch schemes, depends on

    client, project, technology… a common workflow > no workflow at all keep it simple or people won’t follow the scheme
  24. .gitignore know and use .gitignore committed files cannot be ignored

    (well, not easily) https://github.com/github/gitignore take a look at git config and .gitattributes as well
  25. git pull God kills a kitten every time you do

    this: Merge branch 'foobar' of github.com:keradgames/goldenmanager-ios into foobar
  26. git pull git pull is considered harmful git fetch origin

    git rebase origin/develop git config --global pull.ff only when changes can’t be applied fast-forward:
  27. git pull do it for the kittens if you need

    to solve conflicts when pulling, you are doing something wrong
  28. None
  29. pull request why? we all make errors find logic bugs

    high quality code enforce readable code style enforce good commit history let people know what you are working on (even discuss future features) learn (both ways) fun
  30. pull request useful messages: “Can we make this more readable?”

    “What would be a better name for this method?” “This needs to be refactored into smaller methods” “Why did you choose this approach?” “”
  31. Rebase & you

  32. Historical revisionism A B C D E develop feature

  33. Historical revisionism A B C D E develop feature git

    checkout feature git rebase develop
  34. Historical revisionism A B C D E D’ develop feature

    rebasing (1/2)
  35. Historical revisionism A B C D E D’ E’ develop

    feature rebasing (2/2)
  36. Historical revisionism A B C D E D’ E’ develop

    feature rebasing done!
  37. Historical revisionism A B C D E D’ develop feature

    git rebase -i develop
  38. Historical revisionism A B C D E E’ develop feature

    git rebase -i develop
  39. Historical revisionism A B C D E E’ D’ develop

    feature git rebase -i develop
  40. © http://www.maneobjective.com/2013/10/danger-controversial-ingredients-in.html

  41. Rebasing dangers … or when not to use rebase. Don’t

    rebase public history. Think before rebasing long & old branches.
  42. Tips & tricks

  43. How do I… …find the commit that broke my project?

    git bisect start git bisect bad git bisect good <rev> # Bisecting: N revisions left to test after this (roughly log2(N) steps) git bisect run
  44. How do I… …save current changes for later use without

    committing because you need to do something else? git stash git stash list git stash pop git stash apply
  45. How do I… …revert a non-staged change? git checkout --

    path/to/file/to/revert git checkout -- . …revert all non-staged changes (and nothing else)? ⚠ ⚠
  46. How do I… …revert a staged change? git reset path/to/file/to/revert

    git reset …revert all staged changes (and nothing else)?
  47. How do I… …revert all staged and non-staged changes? (i.e.:

    all non-committed changes) git reset --hard ⚠
  48. How do I… …revert the last non-pushed commit? git reset

    HEAD^ …revert/modify a previous non-pushed commit? git rebase -i <parent commit>
  49. How do I… …revert a pushed/public change? git revert <commit

    SHA>
  50. How do I… …copy an existing commit (from another branch

    or another point in history) ? git cherry-pick <commit SHA>
  51. How do I… …split changes in a single file into

    multiple commits? git add -p <file> # for each hunk: y, n, s, e … git add -i
  52. How do I… …clean remote branches? git remote prune origin

    git config --global remote.origin.prune true git push origin --delete <branch_name> …delete a remote branch?
  53. How do I… … remove untracked files and directories? git

    clean -d -f ⚠
  54. How do I… … see changes introduced by a commit?

    git show <commit sha|tag|branch> … see changes introduced by a merge commit? git show -m <commit SHA>
  55. How do I… … find who fucked up modified lines

    42 to 84 of this file? git blame -L 42,84 path/to/file
  56. Questions?

  57. Thanks!