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

Git, Github, and Vim

Git, Github, and Vim

a short training course for Ruby/Rails newcomer

高見龍

April 06, 2013
Tweet

More Decks by 高見龍

Other Decks in Programming

Transcript

  1. Ruby/Rails training course
    Git, Github, and Vim
    eddie@fju

    View Slide

  2. View Slide

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

    View Slide

  4. Git

    View Slide

  5. What's Git

    View Slide

  6. version control system (VCS)

    View Slide

  7. created by Linus Torvalds

    View Slide

  8. Why version control?

    View Slide

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

    View Slide

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

    View Slide

  11. Why Git

    View Slide

  12. free

    View Slide

  13. fast & smaller footprint

    View Slide

  14. commit v.s push

    View Slide

  15. easy to co-work with others

    View Slide

  16. Install Git

    View Slide

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

    View Slide

  18. Exercise:
    please install git in your machine.

    View Slide

  19. How to Git

    View Slide

  20. don't be afraid of command line tools

    View Slide

  21. git clone

    View Slide

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

    View Slide

  23. git init

    View Slide

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

    View Slide

  25. working, staging, and repository

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  29. git status

    View Slide

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

    View Slide

  31. git mv

    View Slide

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

    View Slide

  33. git commit

    View Slide

  34. Exercise:
    just commit it :)

    View Slide

  35. commit message matters!

    View Slide

  36. amend committed message

    View Slide

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

    View Slide

  38. 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

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

    View Slide

  40. git log

    View Slide

  41. Exercise:
    check your commit log

    View Slide

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

    View Slide

  43. git rm

    View Slide

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

    View Slide

  45. git tag

    View Slide

  46. Exercise:
    create a tag for your project

    View Slide

  47. git branch

    View Slide

  48. branching is very cheap

    View Slide

  49. git checkout

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  53. git merge

    View Slide

  54. conflict?

    View Slide

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

    View Slide

  56. git reset
    soft v.s. hard

    View Slide

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

    View Slide

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

    View Slide

  59. git pull

    View Slide

  60. git push

    View Slide

  61. git clean
    remove untracked files

    View Slide

  62. git stash
    apply, pop, list, clear

    View Slide

  63. some dotfiles

    View Slide

  64. .gitconfig
    it should locate in your home directory

    View Slide

  65. 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

  66. make some useful aliases

    View Slide

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

    View Slide

  68. .gitignore
    https://github.com/github/gitignore

    View Slide

  69. Reading References

    View Slide

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

    View Slide

  71. Git Flow

    View Slide

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

    View Slide

  73. 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

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

    View Slide

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

    View Slide

  76. 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

  77. 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

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

    View Slide

  79. Github

    View Slide

  80. What's Github?

    View Slide

  81. a git repository server

    View Slide

  82. coders' facebook :)

    View Slide

  83. View Slide

  84. make friends with awesome coder :)

    View Slide

  85. resume for coders!

    View Slide

  86. Free? Price?

    View Slide

  87. SSH/HTTPS/GIT

    View Slide

  88. How to Github?

    View Slide

  89. Exercise:
    register a new account on Github

    View Slide

  90. without password?

    View Slide

  91. SSH Key

    View Slide

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

    View Slide

  93. Exercise:
    create a new repository on Github

    View Slide

  94. git push

    View Slide

  95. Exercise:
    upload your project to Github

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  101. git pull

    View Slide

  102. Fork

    View Slide

  103. Pull Request

    View Slide

  104. 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

  105. Github Pages

    View Slide

  106. host static files for FREE

    View Slide

  107. upload files via git commands

    View Slide

  108. User pages
    kaochenlong.github.io

    View Slide

  109. Project Pages
    gh-pages branch

    View Slide

  110. custom domain name

    View Slide

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

    View Slide

  112. something else?

    View Slide

  113. bitbucket
    free private repo

    View Slide

  114. git + dropbox

    View Slide

  115. Vim

    View Slide

  116. I'm looking for a editor..

    View Slide

  117. 1. launch quickly
    2. easy to navigate between files
    3. and pretty syntax highlight

    View Slide

  118. What's Vim?

    View Slide

  119. it may look a little old-fashioned

    View Slide

  120. but it can also be modern : )

    View Slide

  121. So, Why Vim?

    View Slide

  122. and, Why not Vim?

    View Slide

  123. How to Vim?

    View Slide

  124. Exercise:
    Install Vim in your machine.
    http://www.vim.org/download.php

    View Slide

  125. .vimrc

    View Slide

  126. if you want to use my .vimrc
    1. clone from my Github repo
    2. cd to cloned repo and execute install script
    3. make symbolic link for vim
    4. done!

    View Slide

  127. https://github.com/kaochenlong/eddie-vim

    View Slide

  128. Modes
    normal, visual, and edit mode

    View Slide

  129. Modes Switch:
    1. in normal mode, "i" or "a" or "o" to enter insert mode(i
    = insert, a = append, o = newline)
    2. in insert mode, ESC or Ctrl+[ to enter normal mode
    3. in normal mode, hit "v" or "V" to enter visual mode
    4. in visual mode, hit ESC or Ctrl+[ to normal mode

    View Slide

  130. ESC or Ctrl + [

    View Slide

  131. View Slide

  132. Exercise:
    launch Vim and switch between normal,
    visual, and edit mode.

    View Slide

  133. Basic

    View Slide

  134. - :w write to file.
    - :q exit Vim.
    - :tabe create a new tab.
    - gt to switch to next tab, gT to previous tab. (I map
    gt and gT to F7 and F8 in my vimrc)
    - :new to create a horizontal split window, :vnew
    or :vs to create a vertical split window.

    View Slide

  135. - vi -o a.rb b.rb to open those two files at the same
    time with horizontal split window.
    - vi -O a.rb b.rb same as above, but in vertical split
    window.
    - vi -p a.rb b.rb to open files with tabs.
    - vi http://www.eddie.com.tw will read the source
    content into vim directly.

    View Slide

  136. Movement

    View Slide

  137. move cursor with h, j, k and l
    disable arrows if possible

    View Slide

  138. - w or W to move cursor forward by a word, and b or
    B is backward.
    - 0 to back to the begin of the line, and $ is jump to
    the end.
    - fx will stop the cursor at the next "x" character in
    current line, and F is search backward.

    View Slide

  139. - } will move cursor to next section, { move to last
    section.
    - vit will visually select content between the tag,
    while vat will even including the tag.
    - vi" will visually select content between current
    double quotes.
    - viw will select the current word.

    View Slide

  140. - gg will move the cursor to the top of the current
    file, and G will jump to bottom.
    - zz, zb, zt
    - / search, n can jump to next matched result, and N
    will jump to last one.
    - * search the word on cursor.

    View Slide

  141. Exercise:
    try to move cursor with hjkl keys, not arrow
    keys.

    View Slide

  142. Buffers
    :ls, :bd, :b3, :b filename, :tab ba

    View Slide

  143. Exercise:
    try to open several files, and switch between
    them with buffer commands.

    View Slide

  144. Editing

    View Slide

  145. - D to clear all content of current line after the
    cursor.
    - C like D, but enter insert mode.
    - dG will clear all content after the cursor, dgg will
    clear all content before the cursor.
    - x remove a character.
    - . to repeat last action.
    - dd to delete whole line.
    - 3dd to delete 3 lines.

    View Slide

  146. - yy yank the whole line of the cursor.
    - 3yy yank 3 lines.
    - p paste content from register.
    - 4p paster content from register for 4 times.
    - = re-format
    - gg=G re-format whole file.

    View Slide

  147. - dw, diw to delete the word in the cursor.
    - cw, ciw same as above, but enter insert mode.
    - r to replace current character.
    - J to concatenate current line with next line, 3J will
    concatenate next 3 lines.
    - > to add indentation, < to remove indentation.

    View Slide

  148. Plugins

    View Slide

  149. Pathogen
    nice plugin manager
    https://github.com/tpope/vim-pathogen

    View Slide

  150. NERDTree
    A tree explorer
    https://github.com/scrooloose/nerdtree

    View Slide

  151. SnipMate
    code snippets plugin, inspired by TextMate
    https://github.com/msanders/snipmate.vim

    View Slide

  152. ctrlp
    Fuzzy file, buffer, mru, tag, etc finder
    https://github.com/kien/ctrlp.vim

    View Slide

  153. surround.vim
    quoting/parenthesizing made simple
    https://github.com/tpope/vim-surround

    View Slide

  154. powerline
    the ultimate vim statusline utility
    https://github.com/Lokaltog/vim-powerline

    View Slide

  155. rails.vim
    Ruby on Rails power tools
    https://github.com/tpope/vim-rails

    View Slide

  156. fugitive
    a git wrapper for vim
    https://github.com/tpope/vim-fugitive

    View Slide

  157. vimwiki
    personal wiki for vim
    https://github.com/vim-scripts/vimwiki

    View Slide

  158. ragtag
    https://github.com/tpope/vim-ragtag

    View Slide

  159. Practicing!

    View Slide

  160. References

    View Slide

  161. http://blog.eddie.com.tw/screencasts/

    View Slide

  162. and more practice.. : )

    View Slide

  163. Tips

    View Slide

  164. in terminal:
    1. ctrl + z to stash vim, and type "fg" to bring it back.
    2. ctrl + w to delete a word, ctrl + u to delete a whole
    line, both work in terminal and vim edit mode.
    3. ctrl + r to find history commands fuzzily.

    View Slide