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

Git and Github: Tips and tricks

Wendy Liu
November 26, 2013

Git and Github: Tips and tricks

Presentation for HackMcGill. Intro to Git/Github plus various tips and tricks, all in one.

Wendy Liu

November 26, 2013
Tweet

More Decks by Wendy Liu

Other Decks in Programming

Transcript

  1. HackMcGill :: January 29, 2014
    tips & tricks
    and
    :

    View Slide

  2. about me
    - Wendy Liu
    - 4th year Math & CS
    - 3 years experience with Git
    - 137-day Github commit streak
    - dellsystem on Twitter, Github, IRC

    View Slide

  3. what is git?

    View Slide

  4. what is git?
    version control system!

    View Slide

  5. svn/perforce: local vs. remote
    comparison to other systems

    View Slide

  6. svn/perforce: local vs. remote
    hg/bzr: faster, more control
    comparison to other systems

    View Slide

  7. svn/perforce: local vs. remote
    hg/bzr: faster, more control
    cvs/rcs: no comparison
    comparison to other systems

    View Slide

  8. backup_dec_1_2012.zip backup_dec_8_2012.zip backup_dec_15_2012.zip
    backup_dec_22_2012.zip backup_dec_29_2012.zip backup_jan_5_2012.zip
    backup_jan_12_2012.zip backup_jan_19_2012.zip backup_jan_26_2012.zip

    View Slide

  9. report_v1.doc report_v2.doc report_v3.doc report_v4.doc

    View Slide

  10. keeps track of changes
    enter git.

    View Slide

  11. keeps track of changes
    extremely flexible
    enter git.

    View Slide

  12. keeps track of changes
    extremely flexible
    developed by linus torvalds
    enter git.

    View Slide

  13. keeps track of changes
    extremely flexible
    developed by linus torvalds
    completely free to use (and open source!)
    enter git.

    View Slide

  14. keeps track of changes
    extremely flexible
    developed by linus torvalds
    completely free to use (and open source!)
    primarily command-line
    enter git.

    View Slide

  15. website
    what is github?

    View Slide

  16. website
    collaborative coding, via git
    what is github?

    View Slide

  17. website
    collaborative coding, via git
    (mostly) free to use
    what is github?

    View Slide

  18. website
    collaborative coding, via git
    (mostly) free to use
    major part of developer ecosystem (esp. OSS)
    what is github?

    View Slide

  19. why git?

    View Slide

  20. why git?
    diffs

    View Slide

  21. why git?
    diffs
    backups

    View Slide

  22. why git?
    diffs
    backups
    collaboration

    View Slide

  23. what git can do for you
    versioning
    easy collaboration
    blame
    tracking down bugs
    safe experimentation
    statistics

    View Slide

  24. what github can do for you
    issue-tracking
    even easier collaboration
    remote backups
    access your files from anywhere
    graphical, easy-to-use UI

    View Slide

  25. now: how git works

    View Slide

  26. repository
    directory on filesystem
    just metadata (changes)
    manual grouping of changes

    View Slide

  27. commit
    group of logically-related changes
    1 or more files (add/delete/modify)

    View Slide

  28. staging index
    CHANGES I WANT IN MY NEXT COMMIT
    ALL THE OTHER CHANGES

    View Slide

  29. git commit -m 'test'
    CHANGES I WANT IN MY NEXT COMMIT
    ALL THE OTHER CHANGES
    a new commit, with message 'test'

    View Slide

  30. CHANGES I WANT IN MY NEXT COMMIT
    ALL THE OTHER CHANGES
    the commit with message 'test'
    (nothing here yet)

    View Slide

  31. your computer
    REMOTE REPOSITORIES
    LOCAL REPOSITORY
    some server another server github

    View Slide

  32. now: git tips

    View Slide

  33. rebasing
    git rebase -i [start commit]
    reorder, reword, edit, squash commits

    View Slide

  34. amending
    git commit --amend
    staged changes become part of previous commit

    View Slide

  35. force pushing
    git push origin master -f
    useful after amending/rebasing

    View Slide

  36. multiple remotes
    git remote add origin [url]
    git remote add upstream [different URL]
    useful when pulling from other developers

    View Slide

  37. patch
    git add --patch
    stage one chunk at a time

    View Slide

  38. diffing
    git diff (unstaged)
    git diff --cached (staged)
    git diff --check (whitespace errors)

    View Slide

  39. colours
    git config --global color.ui auto

    View Slide

  40. fix your whitespace
    git stripspace
    git apply --whitespace=fix

    View Slide

  41. good commit messages
    Fix bug (50 chars max, capitalised, imperative)
    Longer description explaining what the commit does.
    Wrap at 72 characters. Don't forget the blank line.
    bit.ly/gitcommit

    View Slide

  42. bisect
    git bisect (or, "when did this bug first appear?")

    View Slide

  43. ignoring
    .gitignore (untracked files)
    git update-index --assume-unchanged (tracked files)
    git status --ignored

    View Slide

  44. better pulls
    git pull vs git fetch & git merge

    View Slide

  45. aliases
    git s = git status
    git add = git add --patch
    git ds = git diff --staged
    git amend = git commit --amend
    git say = git commit -m
    git un = reset --soft HEAD^1

    View Slide

  46. fixing merge conflicts
    1. Don't panic
    2. git status
    3. Look for the <<<
    4. Stage & commit

    View Slide

  47. moving files
    git mv [source] [destination]
    instead of:
    mv [source] [destination]
    git rm [source]
    git add [destination]

    View Slide

  48. sensitive data
    help.github.com/articles/remove-sensitive-data

    View Slide

  49. selective staging
    git add --update (only tracked files)
    git add --all (all files)

    View Slide

  50. view configuration
    git config --list

    View Slide

  51. pushing to remotes
    git push origin master
    git push origin localbranch:remotebranch
    git push origin :remotebranch (deletes it)

    View Slide

  52. branch tracking
    git push origin master -u
    git push origin local:remote -u

    View Slide

  53. stashing edits
    git stash
    git stash apply
    git stash pop (applies, then removes from stack)

    View Slide

  54. fixing mistakes
    git checkout (undo edits)
    git reset --soft (keeps edits)
    git reset --hard (loses edits)

    View Slide

  55. merge strategies
    git merge --strategy=[strategy]
    strategies: recursive (default), ours, theirs, etc

    View Slide

  56. now: github tips

    View Slide

  57. keyboard shortcuts
    press ? to bring up the help

    View Slide

  58. issues in commits
    "Fix #54" -> closes issue #54 from the commit
    "For #54" -> links to #54 from commit, & vice versa

    View Slide

  59. all from your browser
    edit, commit, send a pull request
    ... all without leaving your browser

    View Slide

  60. blame
    who introduced this line, and when, and why

    View Slide

  61. file rendering
    markup, geoson, csv, and more

    View Slide

  62. integration with travis
    continuous integration (automated testing)

    View Slide

  63. thanks!
    git-scm.com/book
    help.github.com

    View Slide