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

Git and Github

Git and Github

高見龍

May 08, 2013
Tweet

More Decks by 高見龍

Other Decks in Technology

Transcript

  1. Git and Github
    [email protected]

    View Slide

  2. View Slide

  3. Current Status
    80% iOS app, 20% Ruby/Rails

    View Slide

  4. Before we start..

    View Slide

  5. You may create lots of files day by day..

    View Slide

  6. edit them, save, edit them, and save.. x N

    View Slide

  7. Backup?

    View Slide

  8. Ctrl-C + Ctrl-V

    View Slide

  9. "ProjectA" -> "ProjectA20130506"
    -> "ProjectA20130508-1"
    -> "ProjectA20130508-2"
    -> "ProjectA-bak"
    -> "ProjectA-forEddie"

    View Slide

  10. backup and sync on LAN

    View Slide

  11. Version Control

    View Slide

  12. Subversion (SVN)

    View Slide

  13. but the problem is..

    View Slide

  14. when network or svn server is down..

    View Slide

  15. Is there any better way..?

    View Slide

  16. Git

    View Slide

  17. What's Git?

    View Slide

  18. Version Control System (VCS)

    View Slide

  19. created by Linus Torvalds
    for managing Linux kernel source code in 2005

    View Slide

  20. distributed

    View Slide

  21. remote, and also local

    View Slide

  22. Git

    View Slide

  23. Git != Github

    View Slide

  24. Why version control?

    View Slide

  25. backup
    just like you can load saving data while playing RPG game

    View Slide

  26. history and evidence
    you know whom to blame when something is going wrong :)

    View Slide

  27. Why Git?

    View Slide

  28. free

    View Slide

  29. fast & smaller footprint

    View Slide

  30. http://git-scm.com/book/en/Getting-Started-Git-Basics
    some other version control system..

    View Slide

  31. http://git-scm.com/book/en/Getting-Started-Git-Basics
    git

    View Slide

  32. just snapshots, not differences

    View Slide

  33. local commit and remote push

    View Slide

  34. most of the operations are local

    View Slide

  35. easy to co-work with others

    View Slide

  36. But..

    View Slide

  37. git is easy to use, but hard to master

    View Slide

  38. Install Git

    View Slide

  39. on Mac:
    > brew install git
    on Ubuntu or some linux OS:
    > sudo apt-get install git-core
    or
    > sudo apt-get install git

    View Slide

  40. Exercise:
    please install git in your machine.

    View Slide

  41. How to Git

    View Slide

  42. configurations

    View Slide

  43. .gitconfig
    it should locate in your home directory

    View Slide

  44. set your username and email
    > git config --global user.name "eddie"
    > git config --global user.email "[email protected]"
    list all settings
    > git config --list

    View Slide

  45. make some useful aliases

    View Slide

  46. [alias]
    co = checkout
    br = branch
    aa = add --all
    l = "!source ~/.dotfiles/.githelper && pretty_git_log"
    https://github.com/kaochenlong/eddie-dotfiles

    View Slide

  47. Exercise:
    1. set your username and email for git.
    2. edit the ".gitconfig" and add some aliases.

    View Slide

  48. .gitignore
    https://github.com/github/gitignore

    View Slide

  49. don't be afraid of command line tools

    View Slide

  50. git init

    View Slide

  51. Exercise:
    please create a new directory and initialize
    for git version control.

    View Slide

  52. git clone

    View Slide

  53. git clone git://github.com/kaochenlong/eddie-vim.git

    View Slide

  54. Exercise:
    please try to copy a project from Github or
    somewhere to your local machine.

    View Slide

  55. working, staging, and repository

    View Slide

  56. http://git-scm.com/about/staging-area

    View Slide

  57. git add
    can add a single file or all modified files, even a single line.

    View Slide

  58. Exercise:
    add a new file named "hello.rb" and add to
    staging area.

    View Slide

  59. Exercise:
    after adding "hello.rb" to staging area, then
    try to modify it and see what happen?

    View Slide

  60. git status

    View Slide

  61. Exercise:
    try to check if "hello.rb" is in staging area, and
    then remove it from staging area.

    View Slide

  62. git mv

    View Slide

  63. Exercise:
    add "hello.rb" to staging area, and then
    rename it to "world.rb".

    View Slide

  64. git commit

    View Slide

  65. Exercise:
    just commit it :)

    View Slide

  66. When to make a Commit?!

    View Slide

  67. commit message matters!

    View Slide

  68. amend committed message

    View Slide

  69. git commit --amend

    View Slide

  70. Exercise:
    you just committed with a rubbish message,
    pleases amend it to make sense for your
    project.

    View Slide

  71. Exercise:
    in last commit, you forgot to add another file,
    but you don't want to commit again just for
    this single file, please try to commit it with --
    amend.

    View Slide

  72. Notice:
    empty folder won't be committed!

    View Slide

  73. if you still want to commit an empty folder,
    you can put an empty “.gitkeep” file in it by
    convention.

    View Slide

  74. git log

    View Slide

  75. git log --pretty=oneline

    View Slide

  76. git log --pretty=format:"%h %s"

    View Slide

  77. git help log

    View Slide

  78. Exercise:
    check your commit log

    View Slide

  79. Exercise:
    read the help manual of “git log”, and make
    your prefer log format.

    View Slide

  80. Exercise:
    modify something in the "world.rb" then
    commit again.

    View Slide

  81. git rm

    View Slide

  82. Exercise:
    remove a file and then checkout it back.

    View Slide

  83. git tag

    View Slide

  84. tag is a milestone

    View Slide

  85. Exercise:
    create a tag for your project

    View Slide

  86. git branch

    View Slide

  87. branching is very cheap

    View Slide

  88. When to make a Branch?!

    View Slide

  89. git checkout

    View Slide

  90. Exercise:
    1. create a new branch name "fruit"
    2. checkout to "fruit" branch
    3. add a "banana.rb" and commit it

    View Slide

  91. Exercise:
    please try to list all branches, including local
    and remote branches.

    View Slide

  92. Exercise:
    you accidentally delete the "world.rb" file,
    please try to recover it with git commands.

    View Slide

  93. Exercise:
    you just create a tag name “ncku”, try to
    checkout to this tag after several commits.

    View Slide

  94. git merge

    View Slide

  95. conflict?

    View Slide

  96. Exercise:
    1. checkout back to "master" branch
    2. merge "fruit" to "master"
    3. remove "fruit" branch if you like

    View Slide

  97. git reset
    soft v.s. hard

    View Slide

  98. Exercise:
    reset a file to untracked status which you just
    added to staging.

    View Slide

  99. Exercise:
    you just merged a branch, please try reset it
    to back to un-merged branch.

    View Slide

  100. git pull

    View Slide

  101. git pull = git fetch + git merge

    View Slide

  102. git push

    View Slide

  103. git push origin ncku-branch

    View Slide

  104. git clean
    remove untracked files

    View Slide

  105. git stash
    apply, pop, list, clear

    View Slide

  106. git remote

    View Slide

  107. Reading References

    View Slide

  108. 1. Pro Git
    2. ihower's blog http://ihower.tw/git/

    View Slide

  109. Git Flow

    View Slide

  110. http://git-scm.com/book/en/Git-Branching-Branching-Workflows

    View Slide

  111. http://nvie.com/posts/a-successful-git-branching-model/

    View Slide

  112. on Mac:
    > brew install git-flow
    on Ubuntu or some linux OS:
    > sudo apt-get install git-flow
    https://github.com/nvie/gitflow/wiki/Installation

    View Slide

  113. Exercise:
    please install git flow in your machine, and
    initialize a git flow project.

    View Slide

  114. init a project with git flow
    > git flow init

    View Slide

  115. Branches
    Master, Develop, Feature, Release, Hotfix

    View Slide

  116. add a new feature:
    > git flow feature start my_new_feature
    when done with the new feature:
    > git flow feature finish my_new_feature

    View Slide

  117. Exercise:
    your boss ask you to add a new feature which
    can let user upload their photos, please try to
    finish this assignment in git flow.

    View Slide

  118. Exercise:
    your boss find a bug and ask you to fix it
    ASAP, please try to do this assignment in git
    flow.

    View Slide

  119. Github

    View Slide

  120. What's Github?

    View Slide

  121. a git repository server

    View Slide

  122. coders' facebook :)

    View Slide

  123. View Slide

  124. make friends with other awesome coders :)

    View Slide

  125. resume for coders!

    View Slide

  126. Free? Price?

    View Slide

  127. SSH/HTTPS/GIT

    View Slide

  128. How to Github?

    View Slide

  129. Exercise:
    register a new account on Github

    View Slide

  130. without password?

    View Slide

  131. SSH Key

    View Slide

  132. Exercise:
    generate a SSH key pair in your local
    machine and add the public key to Github

    View Slide

  133. Exercise:
    create a new repository on Github

    View Slide

  134. git push

    View Slide

  135. Exercise:
    upload your project to Github

    View Slide

  136. Exercise:
    1. clone a project from Github
    2. do some changes
    3. commit and push back to Github

    View Slide

  137. Exercise:
    1. create a new local branch
    2. add some change and then push this
    branch to Github

    View Slide

  138. tag won't be pushed to repo by default

    View Slide

  139. push a tag:
    > git push origin v2.0
    push several tags:
    > git push origin --tags

    View Slide

  140. Exercise:
    create some tags for your project and upload
    them to Github

    View Slide

  141. git pull

    View Slide

  142. Fork

    View Slide

  143. Pull Request

    View Slide

  144. Exercise:
    1. fork a project from your classmate who is
    sitting just next to you.
    2. add some change and commit.
    3. fire a pull request.

    View Slide

  145. Github Pages

    View Slide

  146. host static files for FREE

    View Slide

  147. upload files via git commands

    View Slide

  148. User pages
    kaochenlong.github.io

    View Slide

  149. Project Pages
    gh-pages branch

    View Slide

  150. custom domain name

    View Slide

  151. Exercise:
    1. create a github page for your account.
    2. set your domain name in CNAME if you
    have one.

    View Slide

  152. something else?

    View Slide

  153. bitbucket
    free private repo

    View Slide

  154. git + dropbox

    View Slide

  155. What's inside the .git folder?

    View Slide