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

ゼロからはじめる Git & GitHub 入門 / challecara-git-hands-on

Tomoka Baba
September 08, 2018

ゼロからはじめる Git & GitHub 入門 / challecara-git-hands-on

資料内リンク

チャレキャラ公式ブログ
https://challecara.hatenablog.com/entry/2018/09/05/052526

Git ダウンロード
https://git-scm.com/downloads

GitHub
https://github.com/

Git をはじめからていねいに - ひとりでつかう - ブランチを知る
https://github.com/takanabe/introduction-to-git/blob/master/05_branch.md

Tomoka Baba

September 08, 2018
Tweet

More Decks by Tomoka Baba

Other Decks in Programming

Transcript

  1. Git ͬͯͳΜͳΜʁ • όʔδϣϯ؅ཧγεςϜͷ1ͭ • Subversion, Mercurial, Visual SourceSafe •

    “෼ࢄܕ”ͷόʔδϣϯ؅ཧγεςϜ • ྫ͑͹Subversion͸"ूதܕ" • ΄ͱΜͲͷ։ൃݱ৔Ͱ࢖ΘΕΔπʔϧ
  2. Ϣʔβʔ৘ใΛ֬ೝ͢Δ ઃఆ Ϣʔβʔ৘ใΛઃఆ͢Δ $ git config --global user.name "Ϣʔβʔ໊" $

    git config --global user.email "ϝʔϧΞυϨε" $ git config --global user.name $ git config --global user.email
  3. ϩʔΧϧϦϙδτϦͷ࡞੒ .git ͕࡞੒͞ΕΔ $ mkdir ~/Desktop/challecara-git $ cd ~/Desktop/challecara-git $

    git init Initialized empty Git repository in ... $ ls -a . . .. .git ※ ͜ΕΛ࡟আ͢ΔͱGitͷ؅ཧ͔Β֎ΕΔ σεΫτοϓʹ ࡞ۀ༻ϑΥϧμΛ࡞Δ
  4. ͸͡Ίͯͷίϛοτ • ࡞ۀϑΥϧμʹ sample.txt Λ࡞੒ $ git status On branch

    master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) sample.txt • git status ίϚϯυΛ࢖ͬͯΈΔ ※ εςʔδϯά͞Εͯͳ͍ঢ়ଶ த਎͸ެ։͞ΕΔͷͰɺ ͦͷͭ΋ΓͰ
  5. ͸͡Ίͯͷίϛοτ εςʔδϯάͯ͠ίϛοτͰ͖ΔΑ͏ʹ͢Δ $ git add sample.txt $ git status On

    branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: sample.txt HJUBEEίϚϯυΛ࢖ͬͯεςʔδϯά
  6. ͸͡Ίͯͷίϛοτ git commit ίϚϯυΛ࢖ͬͯ
 εςʔδϯά͞ΕͨϑΝΠϧΛίϛοτ $ git commit -m "initial

    commit" [master (root-commit) 2586731] initial commit 1 file changed, 1 insertion(+) create mode 100644 sample.txt ※ -m ʹଓ͚ͯίϝϯτΛॻ͘ # ※ Θ͔Γ΍͢ίϝϯτΛॻ͘ͱྑ͍ $
  7. ͸͡Ίͯͷίϛοτ git status ͰϫʔΩϯάσΟϨΫτϦΛ֬ೝ $ git status On branch master

    nothing to commit, working tree clean ϑΝΠϧͷมߋ΋ίϛοτ࿙Ε΋ͳ͍ঢ়ଶ %
  8. ͸͡Ίͯͷίϛοτ git log Ͱ͜Ε·Ͱͷίϛοτه࿥Λ֬ೝ $ git log commit 25867312eexxxxxxxxxxxxxxx (HEAD

    -> master) Author: Hanako Yamada <[email protected]> Date: Wed Sep 5 16:52:29 2018 +0900 initial commit ୭͕͍ͭͲΜͳฤूΛ͔ͨ͠ཤྺ͕࢒Δʂ
  9. ͜͜·Ͱ͓͞Β͍ • git init : ϩʔΧϧϦϙδτϦΛ࡞੒ • git status :

    ϫʔΩϯάσΟϨΫτϦͷঢ়ଶ֬ೝ • git add filepath : ର৅ϑΝΠϧΛεςʔδϯά • git commit -m "comment" : ίϛοτ • git log : ͜Ε·ͰͷίϛοτϩάΛ֬ೝ
  10. ͸͡Ίͯͷϓογϡ $ git push -u origin master Counting objects: 3,

    done. Writing objects: 100% (3/3), 227 bytes | 45.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:TomckySan/challecara-git.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'. ͜Ε·ͰͷίϛοτΛϦϞʔτϦϙδτϦ΁อଘ →ίʔυΛڞ༗͢Δ४උ͕੔ͬͨʂ㊗ git push -u origin master Λ࣮ߦ Ϣʔβʔ໊ͱύεϫʔυͷ ೖྗΛٻΊΒΕͨΒ (JU)VCͷ΋ͷΛೖྗ
  11. ϦϞʔτϦϙδτϦΛڞ༗͢Δ $ cd ~/Desktop $ git clone https://github.com/Username/xxx.git Cloning into

    'xxx'... remote: Counting objects: 3, done. ... લͷεϥΠυͷ ੺࿮ઢ಺ͷ63- σεΫτοϓʹϦϙδτϦ͕࡞੒͞ΕΔ
  12. ϓογϡࣦഊɺɺɺʁ To https://github.com/Username/xxxxx.git ! [rejected] master -> master (fetch first)

    error: failed to push some refs to 'https://github.com/ Username/xxxxx.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push -- help' for details.
  13. ϓογϡࣦഊɺɺɺʁ To https://github.com/Username/xxxxx.git ! [rejected] master -> master (fetch first)

    error: failed to push some refs to 'https://github.com/ Username/xxxxx.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push -- help' for details. ϓογϡ͢Δલʹ ϓϧ͠ΖͬͯݴͬͯΔʁ
  14. ͸͡Ίͯͷϓϧ • ଞͷਓ͕มߋ͍ͯ͠ΔͷͰϓογϡͰ͖ͳ͍ • ϓϧͨ͋͠ͱʹϓογϡ͠Α͏ $ git pull origin master

    ΤσΟλ͕ىಈ͢ΔͷͰɺͦͷ··อଘɾऴྃ →Vim͕ىಈͨ͠৔߹͸ :wq ͱୟ͜͏ • ϓϧͨ͠Β૬खͷϑΝΠϧΛ֬ೝͯ͠ΈΑ͏ • git log Ͱ૬खͷίϛοτϩάΛ֬ೝ͠Α͏
  15. Git Ͱ؅ཧͨ͘͠ͳ͍ϑΝΠϧΛແࢹ͢Δ .gitignore Λ࡞੒͢Δ # .gitignoreͷத਎ .DS_Store *.swp /var/logs/* /build/

    /app/config/parameters.yml .gitignoreʹهड़͞ΕͨϑΝΠϧ͸ Gitͷ؅ཧ͔Β֎͞ΕΔʢadd ΍ commit Ͱ͖ͳ͍ʣ
  16. มߋ಺༰Λ֬ೝ͍ͨ͠ add ͢ΔલͳΒ git diff ͰมߋՕॴ͕֬ೝͰ͖Δ $ git diff $

    git diff --cached add ޙ͸ --cached Λ࢖͏ $ git diff path/to/file.txt ಛఆͷϑΝΠϧ΍σΟϨΫτϦΛࢦఆ͢Δ
  17. มߋ಺༰Λ֬ೝ͍ͨ͠ มߋͷ͋ΔϑΝΠϧ໊͚ͩΛදࣔ͢Δ $ git diff --name-only $ git diff [ൺֱݩͷίϛοτID]

    [ൺֱઌͷίϛοτID] ಛఆͷίϛοτؒͰͷࠩ෼Λදࣔ͢Δ $ git diff HEAD^ ௚લͷίϛοτͱͷࠩ෼Λදࣔ͢Δ
  18. มߋΛऔΓফ͍ͨ͠ add ͢ΔલͳΒ checkout ͰมߋΛऔΓফͤΔ $ git checkout -- sample.txt

    add ͨ͠ޙ, commit ͢ΔલͳΒ reset HEAD Ͱ add͢Δલʹ໭ͤΔ $ git reset HEAD sample.txt
  19. ίϛοτΛऔΓফ͍ͨ͠ ίϛοτΛऔΓফͯ͠ ϑΝΠϧͷมߋ͸ҡ͍࣋ͨ͠৔߹ $ git reset --soft HEAD^ $ git

    reset --hard HEAD^ ίϛοτΛऔΓফͯ͠ ϑΝΠϧͷมߋͦͷ΋ͷ΋ͳ͔ͬͨ͜ͱʹ͍ͨ͠৔߹
  20. มߋΛҰ࣌తʹୀආ͍ͤͨ͞ stash Λ࢖ͬͯมߋΛऔΓফ͠ɺอ͓࣋ͯ͘͠ $ git stash # ୀආͨ͠มߋΛద༻͢Δʢ͜Ε͸ίϝϯτͳͷͰୟ͔ͳ͍Ͱʣ $ git

    stash apply # ୀආͨ͠มߋΛදࣔ $ git stash list # ୀආͨ͠มߋΛ࡟আ $ git stash drop ΄͔߹Θͤͯ࢖͍͍ͨίϚϯυ
  21. git log ͬͯݟʹ͘͘ͳ͍Ͱ͔͢ $ git log --graph ͪΐͬͱୟ͍ͯΈͯ ͪΐͬͱ՚΍͔ʹͳΔ͔΋ $

    git config --global color.ui true γϯϓϧΠζϕετ $ git log --graph --pretty=format:'%s %d'
  22. git log ͬͯݟʹ͘͘ͳ͍Ͱ͔͢ $ git config --global alias.graph "log --graph

    --date-order --all --pretty=format:'%h %Cred%d %Cgreen%ad %Cblue%cn %Creset%s' --date=short" $ git graph ΅͘͸͜͏ͯ͠·͕͢ɺ͜͜Βล͸޷ΈͰ͢ΑͶɻ https://github.com/takanabe/introduction-to-git/ blob/master/05_branch.md
  23. Ͳ͏ͯ͠΋ίϚϯυͰ΍Δͷ͕͠ΜͲ͍ • GUIπʔϧ΋ͨ͘͞Μ͋Γ·͢ • Source Tree • GitHub for Win/Mac

    • git-gui • TortoiseGit • ΤσΟλʹΑͬͯ͸Git࿈ܞϓϥάΠϯ͕
 ͋ͬͨΓ͠·͢
  24. • ϒϥϯνͷ֓೦Λཧղ͠ɺ࢖͍͜ͳ͢ • git checkout • git branch • git

    merge • pull Ͱ͸ͳ͘ fetch ͱ merge Λ࢖ͬͯΈΔ • ϓϧϦΫΤετΛ஌Δ • ΑΓྑ͍Gitͷ࢖͍ํΛௐ΂໛ࡧ͢Δ νʔϜ։ൃΛΑΓԁ׈ʹਐΊΔͨΊʹ