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

Daily Git

Daily Git

A practical dive into everyday-Git

C2bb0454c4af1a61e7f173d54ce17b0b?s=128

Gabriele Petronella

September 29, 2015
Tweet

More Decks by Gabriele Petronella

Other Decks in Programming

Transcript

  1. daily git gabriele petronella software engineer @ buildo Twitter: @gabro27

    / @buildoHQ
  2. None
  3. Version control & me 2010-2011: svn 2010-today: git

  4. git add git commit git push git pull

  5. That's it

  6. git is simple

  7. Questions?

  8. None
  9. git is hard

  10. git is java

  11. None
  12. git is (like) java learning it is part of your

    job
  13. Takeaways » git is powerful, hence hard » git is

    flexible » an idea of the stuff you can do with it
  14. the git model

  15. SVN svn add svn commit git git add git commit

    git push
  16. enter the stage

  17. None
  18. let's start! (with the basics)

  19. 1. create a new repo

  20. 1. create a new repo mkdir darepo cd darepo git

    init git commit --allow-empty -m "[init]" http://stackoverflow.com/questions/435646/combine-the-first-two-commits-of-a-git-repository
  21. 2. check the repo status

  22. 2. check the repo status git status

  23. 3. add changes to the index

  24. 3. add changes to the index git add .gitignore

  25. 3. add changes to the index, the cool way) git

    add -p
  26. 4. save changes

  27. 4. save changes git commit -v

  28. 4. (bonus) save changes, the cool way git commit -p

  29. 5. Browse the history

  30. 5. Browse the history git log

  31. 5. browse the history, examples git log --color git log

    --color --graph git log --color --graph --oneline git log --color --graph --oneline --decorate
  32. 5. browse the history, with style git log --graph --full-history

    --all --color -- pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s %x20%C(yellow)(%an, %C(white)%ar)"
  33. 5. browse the history, à la GitHub git working-history https://gist.github.com/gabro/5883819

  34. 6. save changes remotely

  35. 6. save changes remotely... git remote add origin git@github.com:buildo/git-talk git

    push -u origin master
  36. 6. ...and get them back git pull --ff # friends

    forever <3 shortcut for git fetch git merge --ff origin/master
  37. 7. branching

  38. 7. branching git branch a-new-feature git checkout a-new-feature or better

    git checkout -b a-new-feature
  39. 7. merging a branch git checkout master git merge a-new-feature

  40. 7. clean-up behind us # delete a branch locally (and

    safely) git branch -d a-new-feature # delete a branch remotely git push origin :a-new-feature bonus git branch --merged master | \ grep -v '\\* master' | xargs -n 1 git branch -d
  41. 8. ignore stuff

  42. 8. ignore stuff in .gitignore npm_modules

  43. Hey how do I...

  44. 9. Selectively ignore stuff

  45. 9. Selectively ignore stuff *config.json !example_config.json

  46. 9. (bonus) fun with directories . !"" foo #"" ignore_me

    !"" include_me !"" .gitkeep
  47. 9. (bonus) fun with directories /foo !/foo/include_me vs /foo/* !/foo/include_me

    http://stackoverflow.com/a/20652768/846273
  48. 10. browse the history of a file

  49. 10. browse the history of a file git log --follow

    /path/to/file
  50. 10. browse the history of lines git log -L 1,+10:file.json

  51. 10. browse the history of methods/functions/classes git log -L :methodName:path/to/file

  52. None
  53. 10. browse the history of methods/functions/classes in ~/.gitconfig [core] attributesfile

    = ~/.gitattributes [diff "scala"] xfuncname = "^\\s*(.*(def|object|class|trait) .*)$" in ~/.gitattributes *.scala diff=scala
  54. 11. change the remote repo

  55. 11. change the remote repo git remote --set-url origin https://github.com/gabro/another-repo

  56. 12. move that commit to master?

  57. master feature ! ! ! ! ! ! "##$##% "##$##%

    ! ! ! ! ! ! ! ! &##'##( &##'##( ! ! ! ! "##$##% "##$##% ! ! !xxxxx! ! ! !xxxxx! &##'##( &#####( ! / ! ________/ "##$##% / ! !/ ! ! &#####(
  58. master ! ! "##$##% !ooooo! !ooooo! feature &#####' ! !

    ! ! "##$##% "##$##% ! ! ! ! ! ! ! ! &##(##' &##(##' ! ! ! ! "##$##% "##$##% ! ! !xxxxx! ! ! !xxxxx! &##(##' &#####' ! / ! ________/ "##$##% / ! !/ ! ! &#####'
  59. 12. move that commit to master? git checkout master git

    cherry-pick 231256
  60. 13. move to a branch after I've started working

  61. 13. move to a branch after I've started working Well,

    if you didn't push...
  62. None
  63. 13. move to a branch after I've started working git

    commit -m "stuff" ...oh sh*t!... git branch new-feature # mark this point as the new branch git reset --hard HEAD~1 # rewind master
  64. 14. undo that‽

  65. 14. undo that‽ git regret --hard https://github.com/YtvwlD/git-regret

  66. 14. undo that‽ ⾠ Delete changes to the working directory

    ⾠ git reset --hard ⾠ Restore a file to its last committed version ⾠ git checkout /path/to/file
  67. 14. undo that‽ Undo last local commit and discard changes

    git reset --hard HEAD~1 Undo last local commit git reset --soft HEAD~1
  68. 14. undo that‽ Edit last local commit message git commit

    --amend
  69. 14. undo that‽ Undo last public commit git revert HEAD

  70. 14. undo that‽ Edit last public commit message1 1 git

    commit --amend; git push -f
  71. None
  72. 15. put things away

  73. 15. put things away # stash things away git stash

    -u # get them back git stash [pop|apply]
  74. 16. clean-up branches

  75. 16. clean-up branches git rebase -i <some sha> Bonus: find

    the fork point git merge-base --fork-point master
  76. 17. blame co-workers

  77. 17. blame co-workers git blame /path/to/file

  78. 17. blame co-workers (the cool way) # now it's bad,

    and it was good 10 commits ago git bisect start HEAD HEAD~10 # for each commit we analyze git bisect [good|bad] git bisect log git bisect reset
  79. 18. keep a sane workflow

  80. 18. keep a sane workflow » small features » short-lived

    branches » after merging, no more rebasing » GitHub Flow 2 2 https://guides.github.com/introduction/flow/
  81. useful references » git-scm.com/docs/git-<command> » man git-<command> » try.github.io »

    codeschool.com/paths/git » Git Internals by Scott Chacon 3 3 https://github.com/pluralsight/git-internals-pdf
  82. Thank you

  83. Questions?

  84. work@buildo.io