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

第2回 Git講座

第2回 Git講座

あくあたん工房での第2回 Git講座の講義資料です。gitignore
やrebaseの話、およびブランチ管理とGit GUIクライアントの話をします。

Pudding

April 02, 2018
Tweet

More Decks by Pudding

Other Decks in Programming

Transcript

  1. ໨࣍  (JUͷ࢝Ίํ  GPSLͱϦϞʔτ௥੻ϒϥϯν  HJUJHOPSF Ὂ ԋश 

    ϒϥϯνͷ໋໊نଇ  ίϯϑϦΫτͷղফ Ὂ ԋश  ࡞ۀϒϥϯν͕ϚʔδઌͷDPNNJU
 ͔Β཭Εͯ͠·ͬͨͱ͖ͷରԠ  (JU(6*ΫϥΠΞϯτ
  2. ˔ HJUͷϦϞʔτϦϙδτϦʹؔ܎͢Δ
 ίϚϯυ ˔ ॳճ͸ࣄલʹ࡞੒͓͍ͯͨ͠ϦϞʔτ
 ϦϙδτϦΛ௥Ճ͢ΔͨΊʹ࢖༻͢Δ ˔ ZPVQSPKFDUHJUˠ޷͖ʹಡΈସ͑ͯ remote $

    git remote add origin https://github.com/you/ project.git $ git remote -v origin https://github.com/you/project (fetch) origin https://github.com/you/project (push)
  3. GPSLݩͷมߋΛऔΓࠐΉ ! UPPUTVJUFNBTUPEPO ! ZPVNBTUPEPO 'JYCVH $ git remote add

    upstream https://github.com/tootsuite/ mastodon $ git remote -v | grep upstream upstream https://github.com/tootsuite/mastodon (fetch) upstream https://github.com/tootsuite/mastodon (push) ϦϞʔτ63-Λొ࿥ ʢVQTUSFBNʣ l5PPUzl5XFFUz
  4. GPSLݩͷมߋΛऔΓࠐΉ ! UPPUTVJUFNBTUPEPO ! ZPVNBTUPEPO 'JYCVH $ git fetch upstream

    $ git branch --all | grep upstream remotes/upstream/dev remotes/upstream/master ϦϞʔτϒϥϯνΛ GFUDI l5PPUzl5XFFUz
  5. GPSLݩͷมߋΛऔΓࠐΉ ! UPPUTVJUFNBTUPEPO ! ZPVNBTUPEPO 'JYCVH ϦϞʔτ௥੻ϒϥϯνΛ ϩʔΧϧϒϥϯνʹϚʔδ $ git

    merge upstream master $ git log --pretty=format:”%h: %s” -2 4c0be41: Fix bug 17ceae2: “Toot” -> “Tweet” l5PPUzl5XFFUz
  6. ϒϥϯν·ͱΊ ! ZPVNBTUPEPO NBTUFS EFW NBTUFS EFW ! UPPUTVJUFNBTUPEPO NBTUFS

    EFW ϩʔΧϧ
 ϦϙδτϦ ϦϞʔτ
 ϦϙδτϦ ϦϞʔτϒϥϯν ϩʔΧϧϒϥϯν SFNPUFTPSJHJONBTUFS SFNPUFTPSJHJOEFW SFNPUFTVQTUSFBNNBTUFS SFNPUFTVQTUSFBNEFW ϦϞʔτ௥੻ϒϥϯν GFUDI GFUDI QVTI PSJHJO VQTUSFBN NFSHF
  7. 5JQTlVzΦϓγϣϯ ˔ HJUIVCͰϦϙδτϦΛ࡞Δͱग़ͯ͘Δɺ࠷ॳʹ࣮ߦ
 ͢ΔίϚϯυू Ὂ HJUJOJUHJUSFNPUFBEEPSJHJOIUUQTHJUIVCʜ Ὂ HJUBEEHJUDPNNJUNbJOJU` Ὂ HJUQVTIVPSJHJONBTUFS

    ˔ lVz͸ϩʔΧϧϒϥϯνʹରͯ͠σϑΥϧτͷ
 ϦϞʔτ௥੻ϒϥϯνΛઃఆ͢ΔΦϓγϣϯ Ὂ Ҿ਺ແ͠ͰQVMM΍GFUDIͨ͠ͱ͖ͷऔಘઌΛઃఆ͢Δ
  8. HJU؅ཧͯ͠ཉ͘͠ͳ͍ϑΝΠϧ ˔ ϦϙδτϦʹ͸Ͳ͏ͯ͠΋HJUͰ؅ཧͯ͠ཉ͘͠ͳ͍
 ΋ͷ͕ೖΓࠐΉ͜ͱ͕͋Δ Ὂ QZUIPOͷԾ૝؀ڥʢWFOWʣ Ὂ TRMJUFͷσʔλϕʔεϑΝΠϧ Ὂ ίϯύΠϧͯ͠ग़དྷͨόΠφϦ

    Ὂ FOWϑΝΠϧͳͲൿີͷ৘ใ͕ೖͬͨϑΝΠϧ ˔ BEE͠ͳ͚Ε͹DPNNJU͞ΕΔ͜ͱ͸ͳ͍͕ʜ Ὂ खಈ؅ཧ͸ϛε͕ى͖΍͘͢ɺྑ͘ͳ͍
  9. HJUJHOPSFϑΝΠϧ ˔ HJU؅ཧͨ͘͠ͳ͍ϑΝΠϧ΍σΟϨΫτϦͷϦετ ˔ HJU؅ཧԼͷϦϙδτϦͷϧʔτʹஔ͘ͷ͕Ұൠత ˔ ͜ͷϑΝΠϧࣗମ͸HJU؅ཧ͢Δ͜ͱʹ஫ҙ # Python __pycache__/

    *.pyc # Database file *.sqlite # ignore secret dir src/**/secret ˔ Ͱ࢝·Δߦ͸ίϝϯτ ˔ IPHFͰ֦ுࢠIPHFͷϑΝΠϧ
 શͯΛࢦఆ ˔ ͰݸҎ্ͷϑΝΠϧ΍
 σΟϨΫτϦʹϚον Ὂ TSDTFDSFU΍
 TSDIPHFTFDSFUʹϚον
  10. 5JQT(JUIVC'MPX ˔ (JU'MPXΑΓ΋γϯϓϧͳϑϩʔ ˔ ͭͷӡ༻ϧʔϧ  NBTUFSϒϥϯν͸σϓϩΠՄೳ  NBTUFS͔Β࡞ۀϒϥϯνΛ੾Δ 

    ఆظతʹQVTI͠ɺϩʔΧϧͷόοΫΞοϓΛͱΔ  ϓϧϦΫΤετͱϨϏϡʔΛ༻͍Δ  1VMM3FRVFTUΛϚʔδͨ͠Β௚ͪʹσϓϩΠ͢Δ ˔ EFWϒϥϯν΍IPUpYϒϥϯνɺSFMFBTFϒϥϯνΛ ༻ҙ͠ͳ͍
  11. ίϯϑϦΫτͱ͸ ˔ ฤूͷڝ߹ͷ͜ͱΛࢦ͢ ˔ HJUͷNFSHFૢ࡞ʹΑͬͯࣗಈతʹղফͰ͖ͳ͍มߋ ˔ Α͋͘Δޡղ Ὂ ίϯϑϦΫτΛى͜͢ͷ͸ྑ͘ͳ͍ʂ ˗

    େن໛ͳ։ൃʹͳΕ͹ආ͚ΒΕͳ͍͜ͱɻ
 ద੾ͳରॲΛ֮͑Δ΂͖ Ὂ ڧ੍తʹϚʔδ͢Ε͹ྑ͘ͳ͍ʁ ˗ ͲͪΒ͔ͷϒϥϯν͕໌֬ʹਖ਼͍͠ͳΒͦΕͰ΋ྑ͍
  12. Ͳ͏͍͏ͱ͖ʹى͜Δ͔ʢҰྫʣ NBTUFS CSBODI" CSBODI# +1 branch-A UFTUUYU VQEBUFCZCSBODI" +1 branch-B

    UFTUUYU VQEBUFCZCSBODI# ϑΝΠϧͷ
 ಉ͡৔ॴΛ
 ผͷϒϥϯν͕
 มߋ͍ͯ͠Δ τϐοΫϒϥϯν
  13. ίϯϑϦΫτͤͯ͞ΈΔ ˔ લͷεϥΠυͱಉ͡ঢ়گΛ࠶ݱ͢Δ $ git init && touch test.txt $

    git add test.txt && git commit -m ‘init’ $ git checkout -b branch-A && echo branch-A >> test.txt $ git commit -a -m ‘update by branch-A’ $ git checkout master $ git checkout -b branch-B && echo branch-B >> test.txt $ git commit -a -m ‘update by branch-B’ $ git checkout master $ git merge branch-A $ git merge branch-B Auto-merging test.txt CONFLICT (content): Merge conflict in test.txt Automatic merge failed; fix conflicts and then commit the result. ˠϚʔδ੒ޭ ˠϚʔδࣦഊ
  14. मਖ਼ํ๏ɿखಈϚʔδ ˔ ίϯϑϦΫτ͕ൃੜ͢Δͱɺमਖ਼͢Δ·Ͱଞͷૢ࡞͕ Ͱ͖ͳ͍ ˔ ίϯϑϦΫτͨ͠ϑΝΠϧʹϚʔΫ͕ೖΔ UFTUUYU 1 <<<<<<< HEAD

    2 branch-A 3 ======= 4 branch-B 5 >>>>>>> branch-B ౷߹ϒϥϯνʢNBTUFSʣͷมߋ τϐοΫϒϥϯνʢCSBODI#ʣͷมߋ
  15. मਖ਼ํ๏ɿखಈϚʔδ UFTUUYU 1 <<<<<<< HEAD 2 branch-A 3 ======= 4

    branch-B 5 >>>>>>> branch-B UFTUUYU 1 branch-A 2 branch-B $ git commit -a -m ‘fix conflict’ $ git log --oneline --decorate 1e55543 (HEAD -> master) fix conflict 676fa66 (branch-B) update by branch-B 3f102b2 (branch-A) update by branch-A 72e8ee4 init
  16. मਖ਼ํ๏ɿͲͪΒ͔ʹ߹ΘͤΔ ˔ DIFDLPVUίϚϯυΛ࢖ͬͯ౷߹ϒϥϯν·ͨ͸
 τϐοΫϒϥϯνͷͲͪΒ͔ʹڧ੍తʹ߹ΘͤΔ $ git checkout --ours test.txt $

    cat test.txt branch-A $ git checkout --theirs test.txt $ cat test.txt branch-B ౷߹ϒϥϯνʢࠓճ͸NBTUFSʣʹ߹ΘͤΔ τϐοΫϒϥϯνʢࠓճ͸CSBODI#ʣʹ߹ΘͤΔ
  17. मਖ਼ํ๏ɿఘΊΔʢमਖ਼ͱ͸ʜʣ ˔ ϚʔδΛࣙΊΔ͜ͱ΋ग़དྷΔ ˔ छྨͷঢ়گʹԠͯ͡ίϚϯυΛબͿ # conflictޙʹԿ΋͍ͯ͠ͳ͍৔߹ $ git merge

    --abort # conflictΛमਖ਼͠Α͏ͱ৭ʑ͍ͬͨ͡ޙͷ৔߹ $ git reset --hard HEAD # commit·Ͱ͚ͨ͠Ͳ΍ͬͺΓ΍Ί͍ͨ৔߹ $ git reset --hard ORIG_HEAD
  18. ˔ τϐοΫϒϥϯνʹDIFDLPVU͍ͯ͠Δঢ়ଶͰɺ౷߹ ϒϥϯνʢEFWFMPQʣΛऔΓࠐΉ rebase $ git commit -m 'commit by

    topic' $ git log --oneline --decorate * c98b029 (HEAD -> topic) commit by topic * 72e8ee4 (master) init $ git rebase develop First, rewinding head to replay your work on top of it... Applying: commit by develop $ git log --oneline --decorate * c98b029 (HEAD -> topic) commit by topic * 5104883 (develop) commit by develop * 72e8ee4 (master) init
  19. SFCBTFͰίϯϑϦΫτ͕ى͖ͨΒ ˔ SFCBTF͕׬ྃ͢Δ·ͰԼهͷૢ࡞Λ܁Γฦ͢  ίϯϑϦΫτΛղফʢղফํ๏͸NFSHFͱҰॹʣ  ղফͨ͠ΒDPOUJOVF $ git rebase

    develop # ίϯϑϦΫτϝοηʔδ͸௕͍ͷͰলུ # ԿΒ͔ͷίϯϑϦΫτղফૢ࡞Λͯ͠มߋΛadd͓ͯ͘͠ $ git rebase --continue Applying: commit by develop $ git log --oneline --decorate * c98b029 (HEAD -> topic) commit by topic * 5104883 (develop) commit by develop * 72e8ee4 (master) init
  20. (6*ΫϥΠΞϯτΛ࢖͏ͱָ ˔ ίϚϯυͷΈͩͱർΕΔ͠ϛε͕ग़΍͍͢ ˔ େࣄͳͷ͸ϓϩμΫτ Ὂ (JUܳਓʹͳΓ͍ͨΘ͚Ͱ͸ͳ͍ Ὂ ΋ͪΖΜίϚϯυΛ೺Ѳ͓ͯ͘͜͠ͱ΋େ੾ HJUCSBODIͰ

    ݱࡏͷϒϥϯν Λ֬ೝʜ WJNEJ⒎Ͱมߋͨ͠ ࠩ෼Λ֬ೝͯ͠ʜ ͋ΕɼBEEBMM ͍͍ͯ͠Μ͚ͩͬʁ ίϛοτ*% Կ͚ͩͬʁʁ
  21. ࣗ෼ͷؾʹೖͬͨ΋ͷΛ࢖͓͏ ˔ ͲͷΫϥΠΞϯτ΋Ұ௕Ұ୹ Ὂ ྉۚ Ὂ ͔ͬ͜Α͞ Ὂ ίϯϑϦΫτղফπʔϧ Ὂ

    ࠩ෼දࣔํ๏ Ὂ FUD ˔ *%&ʹඪ४౥ࡌ͞Ε͍ͯΔ΋ͷ͚ͩ Ͱ΋े෼࢖͑Δ
  22. (JU,SBLFO ˔ ྉۚϓϥϯ༗ Ὂ 'SFFඇ঎༻ Ὂ 1SP঎༻ՄɼϚʔδπʔϧ
 ͳͲ Ὂ &OUFSQSJTF(JU)VC

    &OUFSQSJTFͱͷ࿈ܞͳͲ ˔ 8JOEPXT .BD -JOVYରԠ ˔ ͔͍͍ͬ͜ɾ(6*͕ચ࿅͞Εͯ Δ