クックパッド17day技術インターンシップの前半、Gitへんの講義資料です
Gitʹ͍ͭͯ2017-08-08ΫοΫύουαϚʔΠϯλʔϯ 2017@moro ॾڮګհ
View Slide
ॾڮګհ(@moro)Kyosuke MOROHASHI
https://speakerdeck.com/moro/pragprog-and-me
Eisuke Oishi
‣ Git is a free and open source distributedversion control system designed tohandle everything from small to verylarge projects with speed and efficiency.‣ https://git-scm.com/(JU
❌ gitίϚϯυͷ͍ํ⭕ Gitͷ෦ߏ = commitॏཁ⭕ Git(ͱGH(E))ΛͬͨίϥϘϨʔγϣϯ͖ΐ͏͢͜ͱ
https://services.github.com/resources/
Git is a free and open sourcedistributed version control system
version control system
‣ ෳϑΝΠϧΛมߋ͠ͳ͕Β࡞ΔιϑτΣΞʹ͓͍ͯɺ͋Δ࣌ͷʮߏʯΛϦϏδϣϯͱͯ͠อ࣋Ͱ͖Δɻ‣ ҙͷϦϏδϣϯͷ༰Λ෮ݩͰ͖Δɻ‣ ϦϏδϣϯͷཤྺΛอ࣋Ͱ͖Δɻ‣ 3ͭલͷϦϏδϣϯʹΔɺͱ͔Ͱ͖Δɻversion control system: what?
‣ Git is a content-addressable filesystem. Great.‣ see https://git-scm.com/book/en/v2/ | Section 10.2‣ …Ͳ͏͍͏͜ͱͳͷ?“version control” on Git
Git ͷϦϏδϣϯཧcommit, tree, blob ͷ3͕ͭϙΠϯτ
‣ ϑΝΠϧσΟϨΫτϦͷதΛɺϦϙδτϦͷϧʔτ͔ΒḷΔߏͰอ͍࣋ͯ͠Δɻ‣ ϑΝΠϧγεςϜ্ͷσΟϨΫτϦɺϑΝΠϧͱಉ͡‣ σΟϨΫτϦʹͨΔͷ͕ tree‣ ϑΝΠϧʹͨΔͷ͕ blobtree, blob
Figure [email protected] https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
‣ ͋Δ࣌ͷϦϏδϣϯ + ϝλσʔλ͕ commit‣ ͍ͭɺ୭͕ɺͲΜͳϝοηʔδͰίϛοτ͔ͨ͠‣ ϦϏδϣϯͷϧʔτtreeͲΕ͔‣ commitͲΕ͔‣ commitͷੵΈॏͶ = ιϑτΣΞͷཤྺcommit
‣ GitͷϦϏδϣϯཧcommit, tree, blob ͷ3छྨͷgitobjectͰ࣮ݱ͞Ε͍ͯΔɻ‣ ͦΕͧΕͷgit objectsha-1ͰͷidͰࢀরͰ͖Δcommit, tree, blob
‣ ʮ҉߸ֶతϋογϡؔʯͱݺΕΔϋογϡؔɺ·ͨͦͷؔͰऔಘͰ͖Δͷུশɻ‣ ಉ͡σʔλ͔Βಉ͕͡Ͱ͖Δɻ‣ ಉҰͷGitΦϒδΣΫτ͔ΒಉҰͷɺࣅͯΔ͚ͲͪΐͬͱνΨͷ͔Βҧ͏͕औΕΔɻ‣ ΦϒδΣΫτࣝผࢠʹͳΔ(͍ͷͰઌ಄ܻ͕දࣔ͞ΕΔ)ɻFYI: SHA-1
$ git clone [email protected]/fizz-buzz-git.git$ git log --onelinec827de7 Add note99c0455 Init FizzBuzz$ git cat-file -p 99c0455tree 4ddd72269bffbe854007323f6b753e47dd3498aeauthor moro 1466746090 +0900committer moro 1466746090 +0900Init FizzBuzz$ git cat-file -p 4ddd72269bffbe854007323f6b753e47dd3498ae100644 blob fb738ec4422280fa0938c1cf2363f244168ffa5d fizz_buzz.rb$ git cat-file -p fb738ec4422280fa0938c1cf2363f244168ffa5d1.upto(100).each do |n|if (n % 15).zero?puts 'FizuBazu'...commit = tree + ϝλσʔλ
99c0455 4ddd7226 fb738ec442016-06-24 14:28:10Init FizzBuzzfizz_buzz.rb 1.upto(100).each doif (n % 15).zero?puts 'FizuBazu'commit tree blob
$ git log --onelinec827de7 Add note99c0455 Init FizzBuzz$ git cat-file -p c827de7tree 0b6d5f879bab3bdb4856c67a908d430f0bb3f18dparent 99c045537a1deb673a70c72bebbd9a91490068b0author moro 1466746241 +0900committer moro 1466746241 +0900Add note$ git cat-file -p 0b6d5f879bab3bdb4856c67a908d430f0bb3f18d100644 blob 63d8ebbbad7040256bd8f4d12a51df27e75888ce README.md100644 blob fb738ec4422280fa0938c1cf2363f244168ffa5d fizz_buzz.rbcommit = tree + ϝλσʔλ
99c0455c827de74ddd72260b6d5f87fb738ec44fb738ec4463d8ebbba2016-06-24 14:30:41Add note2016-06-24 14:28:10Init FizzBuzzREADME.mdfizz_buzz.rbfizz_buzz.rb 1.upto(100).each doif (n % 15).zero?puts 'FizuBazu'1.upto(100).each doif (n % 15).zero?puts 'FizuBazu'FizzBuzz example tocommit tree blobparent
99c0455c827de74ddd72260b6d5f87fb738ec4463d8ebbba2016-06-24 14:30:41Add note2016-06-24 14:28:10Init FizzBuzzREADME.mdfizz_buzz.rbfizz_buzz.rb 1.upto(100).each doif (n % 15).zero?puts 'FizuBazu'FizzBuzz example tocommit tree blobparent
‣ Git is a content-addressable filesystem. Great.‣ see https://git-scm.com/book/en/v2/ | Section 10.2‣ ͳΔ΄ͲGit Internals
commitcommitblobtreeͷϦϯΫ
tree, blob, commitΘ͔ͬͨɻ΄͔ʹ merge rebase ͱ͍͏ͷΑ͘ฉ͚͘Ͳ?
‣ commit ΛੵΈॏͶΔࢬΛෳཧͰ͖Δbranch
A2A1A3A4B2B1B3X
‣ ෳͷcommitΛʹ࣋ͭcommitΛ࡞͢Δ‣ tree / blobͷதɺ *ݡ͘* ߹ྲྀ͢Δ‣ ࣗಈతʹ߹ྲྀ͖ͤ͞Εͳ͍ͱίϯϑϦΫτmerge ?
A2A1A3A4B2B1B3XX(m)
‣ commitࠩΛผͷى(base)্ʹ࠶(re)ద༻͢Δ‣ ผϒϥϯνͰͭͬͨ͘ػೳΛͱΓࠐΜͩΓ‣ commitࠩΛฤूͨ͠Γআͨ͠Γ·ͱΊͨΓSFCBTF
ϒϥϯνBͰͷ࡞ۀதɺA4Λbaseʹͨ͘͠ͳͬͨA2A1A3A4B2B1B3XA/HEADB/HEAD$ git checkout B $ git rebase Aworkingcopy
A2A1A3A4B2B1B3XA/HEADB2'B1'B3'B/HEADworkingcopyແ໊ϒϥϯνA4ͷ্ʹB1, B2, B3ͱಉͷมߋΛద༻͠ɺͦΕ͕ϒϥϯνBʹͳΔ$ git rebase A
A2A1A3A4XA/HEADB2'B1'B3'B/HEADA3,4B1''B3''ແ໊ϒϥϯν͜ͷ߹ɺ౷߹আͨ͠ίϛοτΛىXͷ্ʹੵΈॏͶͨ৽ͨͳίϛοτ͕Ͱ͖ɺϒϥϯνBͷHEAD͕ߋ৽͞ΕΔɻB2B1B3͖ͬ͞ͷແ໊ϒϥϯν$ git rebase -i X
‣ rebase ͢Ε͍͍ͱ͍͏ͷͰͳ͍‣ commit ͕ผʹͳΔɺࠩద༻ͰϋϚΔɺͳͲ‣ Ұʹ1ͭͣͭ (rebase -i͍ͯ͠Δ࠷தʹ৽ػೳΛೖΕͳ͍)‣ ͖͞΄Ͳͷʮແ໊ϒϥϯνʯɺcommit ͕Θ͔ΕϦϏδϣϯΛ෮ݩͰ͖ΔͷͰ҆৺ɻ‣ $ git reflog ͰݟΒΕΔ࡞ۀίϐʔͷཤྺ͔Β୳ͤΔɻrebase: protip
GitΛͬͨ ίϥϘϨʔγϣϯ
Git is a free and open sourcedistributedversion control system
‣ ଞͷ։ൃऀ͕ͭͬͨ͘ιϑτΣΞͷϦϏδϣϯͷཤྺΛɺωοτϫʔΫӽ͠ʹऔಘͰ͖Δɻ‣ clone: ҾͰࢦఆͨ͠ϦϙδτϦΛऔಘ͢Δ‣ औಘݩΛද͢ϦϞʔτ origin ͕Ճ͞ΕΔclone : ࠷ॳͷऔಘ
‣ ։ൃ͍ͯͬͨ͠ιϑτΣΞͷϦϏδϣϯͷཤྺΛɺ ωοτϫʔΫӽ͠ʹଞͷ։ൃऀͱڞ༗Ͱ͖Δ‣ push: खݩͷ commit(commitΛؚΉ)ΛϦϞʔτͷ ϦϙδτϦʹૹΔ‣ ओϦϞʔτϦϙδτϦ origin ͱ͍͏໊લ͕Ұൠతpush : ࣗͷ commit Λૹ৴
$ git push origin masterpush ͷྫ‣ ϩʔΧϧͷϦϙδτϦͷNBTUFSϒϥϯνʹੵΈॏͶͨDPNNJUͷཤྺΛ‣ PSJHJOͱ͍͏໊લͷϦϞʔτϦϙδτϦͷ‣ NBTUFSϒϥϯνʹૹΔ
$ git push moro 12345678:topic-apush ͷྫ(2)‣ ϩʔΧϧͷϦϙδτϦDPNNJU͔ΒḷΕΔཤྺΛ‣ NPSPͱ͍͏໊લͷϦϞʔτϦϙδτϦͷ‣ UPQJDBϒϥϯνʹૹΔ
$ git remote show origin* remote originFetch URL: [email protected]:moro/clock.gitPush URL: [email protected]:moro/clock.gitHEAD branch: masterRemote branch:master trackedLocal ref configured for 'git push':master pushes to master (up to date)"remote" ͷϦϙδτϦ...?‣ ʮͷ໊લͷϦϞʔτϦϙδτϦʯ
$ git fetch originfetch: clone ͨ͠ޙʹՃ͞Εͨcommitͷऔಘ‣ ϦϞʔτϦϙδτϦͷDPNNJUΛɺϩʔΧϧʹ࣋ͬͯ͘Δ‣ ࣋ͬͯ͘Δ͚ͩɺखݩͷίʔυ ࡞ۀίϐʔมΘΒͳ͍
L2L1L3L4R2R1R3Xworkingcopyผͷਓ͕࡞ͬͨ ίϛοτ
L2L1L3L4R2R1R3Xworkingcopyผͷਓ͕࡞ͬͨ ίϛοτfetch: working copyมΘΒͳ͍
$ git pull originpull: fetch+merge‣ ϦϞʔτϦϙδτϦ͔ΒGFUDI͠ɺNFSHF͢Δ‣ ࡞ۀίϐʔ͕มߋ͞ΕΔ‣ ׳ΕΔ·Ͱ͓͢͢Ί͠ͳ͍‣ pull --rebase ͢ΔͱGFUDISFCBTF‣ ׳ΕΔ·ͰϚδͰ͓͢͢Ί͠ͳ͍
L2L1L3L4R2R1R3Xworkingcopyผͷਓ͕࡞ͬͨ ίϛοτpull: working ͱ remote ͕Ϛʔδ͞ΕΔL5
L2'L1'L3'L4'R2R1R3Xworkingcopyผͷਓ͕࡞ͬͨ ίϛοτpull —rebase : rebase͞ΕΔ = Ln͕ผίϛοτʹͳΔ
GitHub(Enterprise)ͰͷίϥϘϨʔγϣϯ
‣ 1ͭͷϦϙδτϦʹରͯ͠‣ ෳͷϓϩάϥϚ͕‣ ෳͷมߋΛߦ͏GitHub/GHEΛͬͨίϥϘϨʔγϣϯ
‣ มߋ͍ͯ͠Δ࠷தʹɺଞऀͷมߋͱিಥ͠ͳ͍ڥ‣ มߋ༰ΛɺνʔϜͰϨϏϡʔ͢ΔΈ‣ ։ൃ&ϨϏϡʔྃͨ͠มߋΛɺத৺ͷϦϙδτϦʹ ద༻͢ΔΈ‣ த৺ϦϙδτϦʹೖͬͨଞऀͷมߋΛऔಘ͢ΔΈίϥϘϨʔγϣϯʹඞཁͳͷ
cookpad/appworking copy$ git fetch origin$ git checkout origin/master -b topic$ git add ...$ git commit -m ''$ git push -u origin topiccommitcommitclonefetchpushFH(JU)VCqPXͱݺΕ͍ͯΔͷ
cookpad/app- cookpad/app:topicworking copy$ git fetch origin$ git checkout origin/master -b topic$ git add ...$ git commit -m ''$ git push -u origin topiccommitcommitclonefetchpush
cookpad/app- cookpad/app:topicworking copy$ git fetch origin$ git checkout origin/master -b topic$ git add ...$ git commit -m ''$ git push -u origin topiccommitcommitclonefetchpushpull request
cookpad/app- cookpad/app:topicworking copy$ git fetch origin$ git checkout origin/master -b topic$ git add ...$ git commit -m ''$ git push -u origin topiccommitcommitclonefetchpushpull request‣ มߋ͍ͯ͠Δ࠷தʹɺଞऀͷมߋͱিಥ͠ͳ͍ڥ‣ มߋ༰ΛɺνʔϜͰϨϏϡʔ͢ΔΈ‣ ։ൃϨϏϡʔྃͨ͠มߋΛɺத৺ͷ ϦϙδτϦʹద༻͢ΔΈ‣ த৺ϦϙδτϦʹೖͬͨ ଞऀͷมߋΛऔಘ͢ΔΈ
‣ GitHubͳͲʹ͓͍ͯɺαʔό্ͷϦϞʔτϦϙδτϦΛ ෳ͢Δ͜ͱɻ‣ OSS։ൃͰɺதԝϦϙδτϦʹίϛοτݖ͕ͳ͍߹ʹࣗͷมߋΛஔ͘ ϦϞʔτϦϙδτϦΛ࡞Δɻ‣ ΫοΫύουࣾͷαʔϏε։ൃͰ͜ͷϞσϧΛͱ͍ͬͯΔɻ‣ ։ൃऀ͕ଟ͔ͬͨΓɺதԝϦϙδτϦpush -fېࢭͨ͠Γ͢ΔͨΊGPSL
cookpad/appworking copy$ git fetch origin$ git remote add moro [email protected]$ git checkout origin/master -b topic$ git add ...$ git commit -m ''$ git push moro topiccommitcommitclonefetchpushpull requestmoro/app- moro/app:topic
❌ gitίϚϯυͷ͍ํ⭕ Gitͷ෦ߏ = commitॏཁ⭕ Git(ͱGH(E))ΛͬͨίϥϘϨʔγϣϯ͖ΐ͏ͨ͜͠ͱ
‣ 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͋ΘͤͯಡΈ͍ͨ
‣ Git ͿΜͷԋशTDDͱ߹ΘͤͯΓ·͢ɻ‣ ͜͜·ͰͰ࣭͋Εʙ࣭ٙλΠϜ