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

cookpad-17day-tech-internship-2017-git

 cookpad-17day-tech-internship-2017-git

クックパッド17day技術インターンシップの前半、Gitへんの講義資料です

70e13d9877054026fda46d5a5b53a236?s=128

MOROHASHI Kyosuke

August 31, 2017
Tweet

Transcript

  1. Gitʹ͍ͭͯ 2017-08-08 ΫοΫύουαϚʔΠϯλʔϯ 2017 @moro ॾڮګհ 

  2. ॾڮګհ(@moro) Kyosuke MOROHASHI

  3.  https://speakerdeck.com/moro/pragprog-and-me

  4. Eisuke Oishi

  5. ‣ Git is a free and open source distributed version

    control system designed to handle everything from small to very large projects with speed and efficiency. ‣ https://git-scm.com/ (JU 
  6. ❌ gitίϚϯυͷ࢖͍ํ ⭕ Gitͷ಺෦ߏ଄ = commitॏཁ ⭕ Git(ͱGH(E)) Λ࢖ͬͨίϥϘϨʔγϣϯ ͖ΐ͏࿩͢͜ͱ

    
  7.  https://services.github.com/resources/

  8. Git is a free and open source distributed 
 version

    control system 
  9. version control system 

  10. ‣ ෳ਺ϑΝΠϧΛมߋ͠ͳ͕Β࡞Διϑτ΢ΣΞʹ͓͍ͯɺ ͋Δ࣌఺ͷʮߏ੒ʯΛϦϏδϣϯͱͯ͠อ࣋Ͱ͖Δɻ ‣ ೚ҙͷϦϏδϣϯͷ಺༰Λ෮ݩͰ͖Δɻ ‣ ϦϏδϣϯͷཤྺΛอ࣋Ͱ͖Δɻ ‣ 3ͭલͷϦϏδϣϯʹ໭Δɺͱ͔΋Ͱ͖Δɻ version

    control system: what? 
  11. ‣ Git is a content-addressable filesystem. Great. ‣ see https://git-scm.com/book/en/v2/

    | Section 10.2 ‣ …Ͳ͏͍͏͜ͱͳͷ? “version control” on Git 
  12. Git ͷϦϏδϣϯ؅ཧ͸ commit, tree, blob ͷ3͕ͭϙΠϯτ 

  13. ‣ ϑΝΠϧ΍σΟϨΫτϦͷத਎ΛɺϦϙδτϦͷϧʔτ͔ ΒḷΔ໦ߏ଄Ͱอ͍࣋ͯ͠Δɻ ‣ ϑΝΠϧγεςϜ্ͷσΟϨΫτϦɺϑΝΠϧͱಉ͡ ‣ σΟϨΫτϦʹ౰ͨΔͷ͕ tree ‣ ϑΝΠϧʹ౰ͨΔͷ͕

    blob tree, blob 
  14.  Figure 10-1@ https://git-scm.com/book/en/v2/Git-Internals-Git-Objects

  15. ‣ ͋Δ࣌఺ͷϦϏδϣϯ + ϝλσʔλ͕ commit ‣ ͍ͭɺ୭͕ɺͲΜͳϝοηʔδͰίϛοτ͔ͨ͠ ‣ ϦϏδϣϯͷϧʔτtree͸ͲΕ͔ ‣

    ਌commit͸ͲΕ͔ ‣ commitͷੵΈॏͶ = ιϑτ΢ΣΞͷཤྺ commit 
  16. ‣ GitͷϦϏδϣϯ؅ཧ͸commit, tree, blob ͷ3छྨͷgit objectͰ࣮ݱ͞Ε͍ͯΔɻ ‣ ͦΕͧΕͷgit object͸sha-1ͰͷidͰࢀরͰ͖Δ commit,

    tree, blob 
  17. ‣ ʮ҉߸ֶతϋογϡؔ਺ʯͱݺ͹ΕΔϋογϡؔ਺ɺ·ͨ ͸ͦͷؔ਺ͰऔಘͰ͖Δ஋ͷུশɻ ‣ ಉ͡σʔλ͔Β͸ಉ͡஋͕Ͱ͖Δɻ ‣ ಉҰͷGitΦϒδΣΫτ͔Β͸ಉҰͷ஋ɺࣅͯΔ͚Ͳͪΐͬ ͱνΨ΢ͷ͔͸Βҧ͏஋͕औΕΔɻ ‣ ΦϒδΣΫτࣝผࢠʹͳΔ(௕͍ͷͰઌ಄਺ܻ͕දࣔ͞ΕΔ)ɻ

    FYI: SHA-1 
  18. $ git clone git@ghe.example.com/fizz-buzz-git.git $ git log --oneline c827de7 Add

    note 99c0455 Init FizzBuzz $ git cat-file -p 99c0455 tree 4ddd72269bffbe854007323f6b753e47dd3498ae author moro <kyosuke-morohashi@cookpad.com> 1466746090 +0900 committer moro <kyosuke-morohashi@cookpad.com> 1466746090 +0900 Init FizzBuzz $ git cat-file -p 4ddd72269bffbe854007323f6b753e47dd3498ae 100644 blob fb738ec4422280fa0938c1cf2363f244168ffa5d fizz_buzz.rb $ git cat-file -p fb738ec4422280fa0938c1cf2363f244168ffa5d 1.upto(100).each do |n| if (n % 15).zero? puts 'FizuBazu' ... commit = tree + ϝλσʔλ 
  19.  99c0455 4ddd7226 fb738ec44 2016-06-24 14:28:10 Init FizzBuzz fizz_buzz.rb 1.upto(100).each

    do if (n % 15).zero? puts 'FizuBazu' commit tree blob
  20. $ git log --oneline c827de7 Add note 99c0455 Init FizzBuzz

    $ git cat-file -p c827de7 tree 0b6d5f879bab3bdb4856c67a908d430f0bb3f18d parent 99c045537a1deb673a70c72bebbd9a91490068b0 author moro <kyosuke-morohashi@cookpad.com> 1466746241 +0900 committer moro <kyosuke-morohashi@cookpad.com> 1466746241 +0900 Add note $ git cat-file -p 0b6d5f879bab3bdb4856c67a908d430f0bb3f18d 100644 blob 63d8ebbbad7040256bd8f4d12a51df27e75888ce README.md 100644 blob fb738ec4422280fa0938c1cf2363f244168ffa5d fizz_buzz.rb commit = tree + ϝλσʔλ 
  21.  99c0455 c827de7 4ddd7226 0b6d5f87 fb738ec44 fb738ec44 63d8ebbba 2016-06-24 14:30:41

    Add note 2016-06-24 14:28:10 Init FizzBuzz README.md fizz_buzz.rb fizz_buzz.rb 1.upto(100).each do if (n % 15).zero? puts 'FizuBazu' 1.upto(100).each do if (n % 15).zero? puts 'FizuBazu' FizzBuzz example to commit tree blob parent
  22.  99c0455 c827de7 4ddd7226 0b6d5f87 fb738ec44 63d8ebbba 2016-06-24 14:30:41 Add

    note 2016-06-24 14:28:10 Init FizzBuzz README.md fizz_buzz.rb fizz_buzz.rb 1.upto(100).each do if (n % 15).zero? puts 'FizuBazu' FizzBuzz example to commit tree blob parent
  23. ‣ Git is a content-addressable filesystem. Great. ‣ see https://git-scm.com/book/en/v2/

    | Section 10.2 ‣ ͳΔ΄Ͳ Git Internals 
  24.  99c0455 c827de7 4ddd7226 0b6d5f87 fb738ec44 63d8ebbba 2016-06-24 14:30:41 Add

    note 2016-06-24 14:28:10 Init FizzBuzz README.md fizz_buzz.rb fizz_buzz.rb 1.upto(100).each do if (n % 15).zero? puts 'FizuBazu' FizzBuzz example to commit tree blob parent
  25.  commit ਌commit blob tree΁ͷ ϦϯΫ

  26. tree, blob, commit͸Θ͔ͬͨɻ ΄͔ʹ merge ΍ rebase ͱ͍͏ͷ΋ Α͘ฉ͚͘Ͳ? 

  27. ‣ commit ΛੵΈॏͶΔࢬΛෳ਺؅ཧͰ͖Δ branch 

  28.  A2 A1 A3 A4 B2 B1 B3 X

  29. ‣ ෳ਺ͷcommitΛ਌ʹ࣋ͭcommitΛ࡞੒͢Δ ‣ tree / blobͷத਎͸ɺ *ݡ͘* ߹ྲྀ͢Δ ‣ ࣗಈతʹ߹ྲྀ͖ͤ͞Εͳ͍ͱίϯϑϦΫτ

    merge ? 
  30.  A2 A1 A3 A4 B2 B1 B3 X X(m)

  31. ‣ commitࠩ෼Λผͷى఺(base)্ʹ࠶(re)ద༻͢Δ ‣ ผϒϥϯνͰͭͬͨ͘ػೳΛͱΓࠐΜͩΓ ‣ commitࠩ෼Λฤूͨ͠Γ࡟আͨ͠Γ·ͱΊͨΓ SFCBTF 

  32. ϒϥϯνBͰͷ࡞ۀதɺA4Λbaseʹͨ͘͠ͳͬͨ  A2 A1 A3 A4 B2 B1 B3 X

    A/HEAD B/HEAD $ git checkout B
 $ git rebase A working copy
  33.  A2 A1 A3 A4 B2 B1 B3 X A/HEAD

    B2' B1' B3' B/HEAD working copy ແ໊ϒϥϯν A4ͷ্ʹB1, B2, B3ͱಉ౳ͷมߋΛద༻͠ɺ ͦΕ͕ϒϥϯνBʹͳΔ $ git rebase A
  34.  A2 A1 A3 A4 X A/HEAD B2' B1' B3'

    B/HEAD A3,4 B1'' B3'' ແ໊ϒϥϯν ͜ͷ৔߹΋ɺ౷߹࡟আͨ͠ίϛοτΛ ى఺Xͷ্ʹੵΈॏͶͨ৽ͨͳίϛοτ͕Ͱ͖ɺ ϒϥϯνBͷHEAD͕ߋ৽͞ΕΔɻ B2 B1 B3 ͖ͬ͞ͷ ແ໊ϒϥϯν $ git rebase -i X
  35. ‣ rebase ͢Ε͹͍͍ͱ͍͏΋ͷͰ͸ͳ͍ ‣ commit ͕ผ෺ʹͳΔɺࠩ෼ద༻ͰϋϚΔɺͳͲ ‣ Ұ౓ʹ1ͭͣͭ (rebase -i͍ͯ͠Δ࠷தʹ৽ػೳΛೖΕͳ͍)

    ‣ ͖͞΄Ͳͷʮແ໊ϒϥϯνʯ΋ɺcommit ͕Θ͔Ε͹ϦϏδϣ ϯΛ෮ݩͰ͖ΔͷͰ҆৺ɻ ‣ $ git reflog ͰݟΒΕΔ࡞ۀίϐʔͷཤྺ͔Β୳ͤΔɻ rebase: protip 
  36. GitΛ࢖ͬͨ
 ίϥϘϨʔγϣϯ 

  37. Git is a free and open source distributed version control

    system 
  38. ‣ ଞͷ։ൃऀ͕ͭͬͨ͘ιϑτ΢ΣΞͷϦϏδϣϯͷཤྺΛɺ ωοτϫʔΫӽ͠ʹऔಘͰ͖Δɻ ‣ clone: Ҿ਺Ͱࢦఆͨ͠ϦϙδτϦΛऔಘ͢Δ ‣ औಘݩΛද͢ϦϞʔτ origin ͕௥Ճ͞ΕΔ

    clone : ࠷ॳͷऔಘ 
  39. ‣ ։ൃ͍ͯͬͨ͠ιϑτ΢ΣΞͷϦϏδϣϯͷཤྺΛɺ
 ωοτϫʔΫӽ͠ʹଞͷ։ൃऀͱڞ༗Ͱ͖Δ ‣ push: खݩͷ commit(਌commitΛؚΉ)ΛϦϞʔτͷ
 ϦϙδτϦʹૹΔ ‣ ओϦϞʔτϦϙδτϦ͸

    origin ͱ͍͏໊લ͕Ұൠత push : ࣗ෼ͷ commit Λૹ৴ 
  40. $ git push origin master push ͷྫ ‣ ϩʔΧϧͷϦϙδτϦͷNBTUFSϒϥϯνʹੵΈॏͶͨDPNNJUͷཤྺΛ ‣

    PSJHJOͱ͍͏໊લͷϦϞʔτϦϙδτϦͷ ‣ NBTUFSϒϥϯνʹૹΔ 
  41. $ git push moro 12345678:topic-a push ͷྫ(2) ‣ ϩʔΧϧͷϦϙδτϦDPNNJU͔ΒḷΕΔཤྺΛ ‣

    NPSPͱ͍͏໊લͷϦϞʔτϦϙδτϦͷ ‣ UPQJDBϒϥϯνʹૹΔ 
  42. $ git remote show origin * remote origin Fetch URL:

    git@ghe.example.com:moro/clock.git Push URL: git@ghe.example.com:moro/clock.git HEAD branch: master Remote branch: master tracked Local ref configured for 'git push': master pushes to master (up to date) "remote" ͷϦϙδτϦ...? ‣ ʮͷ໊લͷϦϞʔτϦϙδτϦʯ 
  43. $ git fetch origin fetch: clone ͨ͠ޙʹ௥Ճ͞Εͨcommitͷऔಘ ‣ ϦϞʔτϦϙδτϦͷDPNNJUΛɺϩʔΧϧʹ࣋ͬ ͯ͘Δ

    ‣ ࣋ͬͯ͘Δ͚ͩɺखݩͷίʔυ ࡞ۀίϐʔ ͸มΘ Βͳ͍ 
  44.  L2 L1 L3 L4 R2 R1 R3 X working

    copy ผͷਓ͕࡞ͬͨ
 ίϛοτ
  45.  L2 L1 L3 L4 R2 R1 R3 X working

    copy ผͷਓ͕࡞ͬͨ
 ίϛοτ fetch: working copyมΘΒͳ͍
  46. $ git pull origin pull: fetch+merge ‣ ϦϞʔτϦϙδτϦ͔ΒGFUDI͠ɺNFSHF͢Δ ‣ ࡞ۀίϐʔ͕มߋ͞ΕΔ

    ‣ ׳ΕΔ·Ͱ͸͓͢͢Ί͠ͳ͍ ‣ pull --rebase ͢ΔͱGFUDI SFCBTF ‣ ׳ΕΔ·Ͱ͸ϚδͰ͓͢͢Ί͠ͳ͍ 
  47.  L2 L1 L3 L4 R2 R1 R3 X working

    copy ผͷਓ͕࡞ͬͨ
 ίϛοτ pull: working ͱ remote ͕Ϛʔδ͞ΕΔ L5
  48.  L2' L1' L3' L4' R2 R1 R3 X working

    copy ผͷਓ͕࡞ͬͨ
 ίϛοτ pull —rebase : rebase͞ΕΔ = Ln͕ผίϛοτʹͳΔ
  49. GitHub(Enterprise)Ͱͷ ίϥϘϨʔγϣϯ 

  50. 

  51. ‣ 1ͭͷϦϙδτϦʹରͯ͠ ‣ ෳ਺ͷϓϩάϥϚ͕ ‣ ෳ਺ͷมߋΛߦ͏ GitHub/GHEΛ࢖ͬͨίϥϘϨʔγϣϯ 

  52. ‣ มߋ͍ͯ͠Δ࠷தʹɺଞऀͷมߋͱিಥ͠ͳ͍؀ڥ ‣ มߋ಺༰ΛɺνʔϜͰϨϏϡʔ͢Δ࢓૊Έ ‣ ։ൃ&ϨϏϡʔ׬ྃͨ͠มߋΛɺத৺ͷϦϙδτϦʹ
 ద༻͢Δ࢓૊Έ ‣ த৺ϦϙδτϦʹೖͬͨଞऀͷมߋΛऔಘ͢Δ࢓૊Έ ίϥϘϨʔγϣϯʹඞཁͳ΋ͷ

    
  53.  cookpad/app working copy $ git fetch origin $ git

    checkout origin/master
 -b topic $ git add ... $ git commit -m '' $ git push -u origin topic commit commit clone fetch push FH(JU)VCqPXͱݺ͹Ε͍ͯΔ΋ͷ
  54.  cookpad/app - cookpad/app:topic working copy $ git fetch origin

    $ git checkout origin/master
 -b topic $ git add ... $ git commit -m '' $ git push -u origin topic commit commit clone fetch push
  55.  cookpad/app - cookpad/app:topic working copy $ git fetch origin

    $ git checkout origin/master
 -b topic $ git add ... $ git commit -m '' $ git push -u origin topic commit commit clone fetch push pull request
  56.  cookpad/app - cookpad/app:topic working copy $ git fetch origin

    $ git checkout origin/master
 -b topic $ git add ... $ git commit -m '' $ git push -u origin topic commit commit clone fetch push pull request ‣ มߋ͍ͯ͠Δ࠷தʹɺଞऀͷมߋͱিಥ͠ͳ͍؀ڥ ‣ มߋ಺༰ΛɺνʔϜͰϨϏϡʔ͢Δ࢓૊Έ ‣ ։ൃϨϏϡʔ׬ྃͨ͠มߋΛɺத৺ͷ
 ϦϙδτϦʹద༻͢Δ࢓૊Έ ‣ த৺ϦϙδτϦʹೖͬͨ
 ଞऀͷมߋΛऔಘ͢Δ࢓૊Έ
  57. ‣ GitHubͳͲʹ͓͍ͯɺαʔό্ͷϦϞʔτϦϙδτϦΛ
 ෳ੡͢Δ͜ͱɻ ‣ OSS։ൃͰɺதԝϦϙδτϦʹίϛοτݖ͕ͳ͍৔߹ʹࣗ෼ͷมߋΛஔ͘
 ϦϞʔτϦϙδτϦΛ࡞Δɻ ‣ ΫοΫύουࣾ಺ͷαʔϏε։ൃͰ΋͜ͷϞσϧΛͱ͍ͬͯΔɻ ‣ ։ൃऀ͕ଟ͔ͬͨΓɺதԝϦϙδτϦ͸push

    -fېࢭͨ͠Γ͢ΔͨΊ GPSL 
  58.  cookpad/app working copy $ git fetch origin $ git

    remote add moro git@.... $ git checkout origin/master
 -b topic $ git add ... $ git commit -m '' $ git push moro topic commit commit clone fetch push pull request moro/app - moro/app:topic
  59. ❌ gitίϚϯυͷ࢖͍ํ ⭕ Gitͷ಺෦ߏ଄ = commitॏཁ ⭕ Git(ͱGH(E)) Λ࢖ͬͨίϥϘϨʔγϣϯ ͖ΐ͏࿩ͨ͜͠ͱ

    
  60. ‣ 1SP(JUCPPL ‣ ެࣜαΠτʹͯɺॻ੶ͷ׬શ൛ ๜༁෇͖ ͕͋ΔɻҰಡ͢ΔͱΑ͍ɻ ‣ https://git-scm.com/book/ja/v2 ‣ ʲ຋༁ʳ(JUΛϘτϜΞοϓ͔Βཧղ͢Δ

    ‣ ࠓ೔΍ͬͨΑ͏ʹDPNNJUΦϒδΣΫτΛ௨ͯ͡(JUΛཧղ͢Δهࣄɻ
 ݸਓతʹ͸ͱͯ΋໾ʹཱͬͨ ‣ http://keijinsonyaban.blogspot.jp/2011/05/git.html ‣ લ৬ͷษڧձͰಉ͡Α͏ͳࢿྉΛ࡞ͬͯ·ͨ͠ɻ΍͸ΓDPNNJUͷղઆ͕ଟ͍ ‣ https://speakerdeck.com/moro/my-mental-model-about-git ͋ΘͤͯಡΈ͍ͨ 
  61. ‣ Git ͿΜͷԋश͸TDDͱ߹Θͤͯ΍Γ·͢ɻ ‣ ͜͜·ͰͰ࣭໰͋Ε͹ʙ ࣭ٙλΠϜ