かたつむりにもわかる Git 超入門

かたつむりにもわかる Git 超入門

4b110d33674fe1a45080f5ddf77809e0?s=128

Keisuke SATO

June 01, 2014
Tweet

Transcript

  1. ͔ͨͭΉΓʹ΋Θ͔Δ Git ௒ೖ໳ Keisuke SATO a.k.a. riaf

  2. @riaf ࠤ౻Ղ༞ (Keisuke SATO) 1987/9/16 ! ๺ւಓ۴࿏ग़਎ ۴࿏ބྕ → ๺ݟ޻ۀେֶ

    ! Co-Founder and Engineer at Crocos Inc. ! LOCAL Japan PHP Users Group Sapporo.php ! Love Ohotech !
  3. Love Hokkaido, from Tokyo.

  4. ͔ͨͭΉΓ…ʁ

  5. ͳ͔ͥͨͭΉΓ޲͚ͷ࿩Λ͢Δ͜ͱʹͳ͔ͬͨ

  6. None
  7. ๻ʹ༩͑ΒΕͨ;ͨͭͷબ୒ࢶ

  8. None
  9. None
  10. None
  11. ͔ͨͭΉΓ…ʁ

  12. ͔ͨͭΉΓʹ΋Θ͔Δ Git ௒ೖ໳ Keisuke SATO a.k.a. riaf

  13. ͦͷલʹ΋͏Ұݴ

  14. Thanks! Ohotech. ελοϑͷํʑɺ͓ർΕ͞·Ͱ͢ɻ ͋Γ͕ͱ͏͍͟͝·͢ɻ ! ԕ͍Ͱ͕͢ɺԠԉ͓ͯ͠Γ·͢ʂ Կ͔͋ͬͨΒ͓ख఻͍͠·͢ͷͰ ͓ؾܰʹʂ

  15. ͔ͨͭΉΓʹ΋Θ͔Δ Git ௒ೖ໳ Keisuke SATO a.k.a. riaf

  16. ͸͡Ίʹ

  17. Git ͱ͸ ? • GitʢΪοτʣ͸ɺϓϩάϥϜͷιʔείʔυͳͲͷม ߋཤྺΛه࿥ɾ௥੻͢ΔͨΊͷ෼ࢄܕόʔδϣϯ؅ཧ γεςϜͰ͋Δɻ΋ͱ͸LinuxΧʔωϧͷιʔείʔυ ؅ཧʹ༻͍ΔͨΊʹϦʔφεɾτʔόϧζʹΑͬͯ։ ൃ͞ΕɺͦΕҎ߱΄͔ͷଟ͘ͷϓϩδΣΫτͰ࠾༻͞ Ε͍ͯΔɻLinuxΧʔωϧͷΑ͏ͳڊେϓϩδΣΫτʹ

    ΋ରԠͰ͖ΔΑ͏ʹɺಈ࡞଎౓ʹॏ఺͕ஔ͔Ε͍ͯΔɻ ݱࡏͷϝϯςφϯε͸ᖛ໺७ (Junio C Hamano) ͕୲౰ ͍ͯ͠Δɻ Wikipedia contributors. "Git." Wikipedia. Wikipedia, 9 Mar. 2014. Web. 31 May. 2014.
  18. όʔδϣϯΛ؅ཧ͢Δ • ϑΝΠϧͷมߋʢ࡞੒ɾ࡟আʣΛه࿥͢Δ • ͍ͭɺͩΕ͕ɺͲͷΑ͏ʹʁ • ҎલͷόʔδϣϯͷϑΝΠϧΛऔΓग़ͤΔ • όοΫΞοϓతʹ࢖͑Δ

  19. ͳͥόʔδϣϯ؅ཧ͕ඞཁͳͷ͔

  20. ͳͥόʔδϣϯ؅ཧ͕ඞཁͳͷ͔ ࠷ѱͷࣄଶ

  21. ਓ͕ؒ؅ཧ͢Δͷʹ͸ݶք͕͋Δ • ؾ߹͍͕͋Δ͏ͪ͸͍͍Μ͚ͩͲ • Ұ౓खΛൈ͘ͱ஍ࠈͰ͢ • ΈΜͳେମ͍͍ՃݮͩΑͶ

  22. ͳͥ Git ͳͷ͔

  23. ͳͥ Git ͳͷ͔ • ͍Ζ͍Ζཧ༝͸͋Γ·͕͢ɺ๻͸ • GitHub ͕͋Δ • ΦʔϓϯιʔεͷίϥϘϨʔγϣϯ

    • Linus ͕࡞ͬͨ • ઃܭ͕γϯϓϧ • ͜ͷ 2 ͕ͭେ͖ͳϙΠϯτ͔ͳͬͯࢥ͍ͬͯ·͢
  24. ͸͡ΊΑ͏ Git Git Λར༻͢Δʹ͓֮͋ͨͬͯ͑ͯ͘ͱྑ͍ΩʔϫʔυͳͲͳͲ

  25. ͸͡Ίʹ • Git ͸ GUI ͷΞϓϦ΋͋Γ·͕͢ɺ࣮ࡍʹͲ͏͍͏͜ ͱ͕ߦΘΕΔͷ͔ΛΠϝʔδͰ͖ΔΑ͏ʹͳΔͨΊʹɺ ͜ͷࢿྉͷதͰ͸ίϚϯυΛ͔ͭͬͯઆ໌͠·͢ɻ • GUI

    ΞϓϦͰͰ͖Δ͜ͱ͸ɺ͢΂ͯ͜ͷίϚϯυ·ͨ ͸ίϚϯυͷ૊Έ߹ΘͤͰ࣮ݱͰ͖·͢ɻ • ͱ͸͍͑ɺσβΠφʔͷํ͸ίϚϯυΛ೔ৗతʹ࢖Θ ͳ͍ਓ͕ଟ͍ͱࢥ͏ͷͰɺ࣮ࡍʹ͸Կ͔͠Βͷιϑτ ͷػೳΛ࢖͏͜ͱ͕ଟ͍ͷ͔ͳͱࢥ͍·͢ɻ
  26. ͸͡Ίʹ • ͜ͷ͓࿩͸ Git ೖ໳ͱͯ͠·͕͢ɺ͜ͷ࿩Λฉ͍ͨΒ Git ͕࢖͑ΔΑ͏ʹͳΔʂͱ͍͏ΑΓ͸ɺGit ͕Կऀͳ ͷ͔ͪΐͬͱ͚ͩΘ͔Δɻ͘Β͍ͷ͓࿩Ͱ͢ɻ •

    Git ͷٕज़తͳ͓࿩͸͠·ͤΜɻGit 2.0 ϦϦʔε͞Ε ͨͶʂͱ͔ͷτϐοΫ͸ग़͖ͯ·ͤΜɻ • ༻ޠͷਖ਼͠͞ΑΓɺ఻ΘΓ΍͢͞Λج४ʹݴ༿ΛબͿ ͜ͱ͕͋Γ·͢ɻݰਓ͸ͪΐͬͱͷ͍͋ͩզຫ͍ͯ͠ ͍ͯͩ͘͞ɻ
  27. $ ͯ͞ɺ͓ͳ͡Έͷࠇ͍ը໘Ͱ͢ɻ ͜ͷ $ ͸ɺίϚϯυೖྗΛ଴ػ͍ͯ͠Δ ঢ়ଶͩͱࢥ͍ͬͯͩ͘͞ɻ ʢͭ·ΓɺԿ΋ೖྗ͞Ε͍ͯͳ͍ɻͱ͍͏͜ͱͰ͢ʣ

  28. $ ls ͯ͞ɺ͓ͳ͡Έͷࠇ͍ը໘Ͱ͢ɻ ls ίϚϯυΛೖྗͨ͠ঢ়ଶͰ͢

  29. $ ls ↵ ͯ͞ɺ͓ͳ͡Έͷࠇ͍ը໘Ͱ͢ɻ ΤϯλʔΩʔΛԡͨ͜͠ͱΛ໌֬ʹ͢ΔͨΊʹ ʮ ↵ ʯͷه߸Λ࢖͍ͬͯ·͢ɻ ʢࢿྉ಺ͰపఈͰ͖ͳ͍͔΋͠Εͳ͍͚Ͳ͝ΊΜʣ

  30. $ ls ↵ hokkaido.jpg kitami.jpg kushiro.jpg sapporo.txt ͯ͞ɺ͓ͳ͡Έͷࠇ͍ը໘Ͱ͢ɻ ৽ͨͳߦʹදࣔ͞ΕΔͷ͕ ls

    ίϚϯυͷग़ྗ݁ՌͰ͢ɻ
  31. $ echo “ͦΕͰ͸ɺ࢝Ί·͢Αʂ" ↵ ͦΕͰ͸ɺ࢝Ί·͢Αʂ

  32. ! ! ! ! ! $ mkdir Ohotech ·ͣ͸ɺόʔδϣϯ؅ཧΛ͍ͨ͠σΟϨΫτϦʢϑΥϧ μʣΛ༻ҙ͠·͢ɻ΋ͪΖΜɺطʹ͋Δ΋ͷͰ΋͔·͍

    ·ͤΜɻ σΟϨΫτϦΛ࡞ΔίϚϯυ
  33. ! ! ! ! ! $ mkdir Ohotech ↵ $

    cd Ohotech ·ͣ͸ɺόʔδϣϯ؅ཧΛ͍ͨ͠σΟϨΫτϦʢϑΥϧ μʣΛ༻ҙ͠·͢ɻ΋ͪΖΜɺطʹ͋Δ΋ͷͰ΋͔·͍ ·ͤΜɻ σΟϨΫτϦΛ։͘ίϚϯυ
  34. ! ! ! ! ! $ mkdir Ohotech ↵ $

    cd Ohotech ↵ $ git init ·ͣ͸ɺόʔδϣϯ؅ཧΛ͍ͨ͠σΟϨΫτϦʢϑΥϧ μʣΛ༻ҙ͠·͢ɻ΋ͪΖΜɺطʹ͋Δ΋ͷͰ΋͔·͍ ·ͤΜɻ σΟϨΫτϦͷྺ࢙Λ؅ཧ͠ ࢝Ί·͢Αɺͱ͍͏એݴ
  35. ! ! ! ! ! $ mkdir Ohotech ↵ $

    cd Ohotech ↵ $ git init ↵ Initialized empty Git repository in /path/to/ Ohotech/.git/ ·ͣ͸ɺόʔδϣϯ؅ཧΛ͍ͨ͠σΟϨΫτϦʢϑΥϧ μʣΛ༻ҙ͠·͢ɻ΋ͪΖΜɺطʹ͋Δ΋ͷͰ΋͔·͍ ·ͤΜɻ /path/to/Ohotech/.git/ ʹۭͷϦϙδτϦ͕࡞੒͞Ε·͠ ͨɻ “.” (υοτ) ͔Β࢝·ΔϑΝΠϧ໊͸ɺ௨ৗݟ͑ͳ ͍ϑΝΠϧͰ͢ (Windows ͸ؔ܎ͳ͍Ͱ͕͢)
  36. ! ! ! ! ! $ ls ↵ test.txt $

    cat test.txt ↵ This is a pan. $ git add test.txt ↵ ࣍ʹɺԿ͔͠ΒͷϑΝΠϧΛ௥Ճͯ͠Έ·͠ΐ͏ɻ ͜͜Ͱ͸ “test.txt” ͱ͍͏ϑΝΠϧΛ࡞Γ·ͨ͠ɻ ← ϑΝΠϧͷத਎Λදࣔ͢ΔίϚϯυ “test.txt” Λ git Ͱ؅ཧ࢝͠Ί·͢Αɺͱ͍͏એݴ
  37. ! ! ! ! ! $ git commit -m “test.txt

    Λ௥Ճ͠·ͨ͠” ↵ ࣍ʹɺʮϑΝΠϧΛ௥Ճͨ͠ʯ͜ͱΛ git ʹه࿥ͤ͞· ͢ɻ git ϦϙδτϦͷঢ়ଶΛه࿥͢ΔίϚϯυ
  38. ! ! ! ! ! $ git commit -m “test.txt

    Λ௥Ճ͠·ͨ͠” ↵ ࣍ʹɺʮϑΝΠϧΛ௥Ճͨ͠ʯ͜ͱΛ git ʹه࿥ͤ͞· ͢ɻ มߋʹରͯ͠ίϝϯτΛ͚͍ͭͯ·͢ɻ
  39. ! ! ! ! ! $ git commit -m “test.txt

    Λ௥Ճ͠·ͨ͠” ↵ [master (root-commit) 026c173] test.txt Λ௥Ճ͠· ͨ͠ 1 file changed, 1 insertion(+) create mode 100644 test.txt ࣍ʹɺʮϑΝΠϧΛ௥Ճͨ͠ʯ͜ͱΛ git ʹه࿥ͤ͞· ͢ɻ 1 ͭͷϑΝΠϧ͕มߋ͞Εɺ 1 ߦ͕௥Ճ͞Ε·ͨ͠ɻ
  40. มߋΛه࿥͢Δͱ͍͏͜ͱ • جຊతʹ͸ɺ͜ͷҰ࿈ͷૢ࡞Λߦ͏͜ͱͰʮ͍ͭʯʮͩ Ε͕ʯʮͲͷΑ͏ͳʯมߋΛߦ͔ͬͨΛه࿥͢Δ͜ͱ ͕Ͱ͖·͢ɻ • ग़͖ͯͨίϚϯυ͸ • git init

    - git ϦϙδτϦΛ࡞Δ • git add - ؅ཧ͢ΔϑΝΠϧΛ௥Ճ͢Δ • git commit - มߋΛه࿥͢Δ
  41. มߋΛه࿥͢Δͱ͍͏͜ͱ • ʮ͑ɺฅΊΜͲͤ͑͘…ʯͱࢥ͔ͬͨ΋͠Ε·ͤΜ͕ɺ ׳Ε·͢ɻ • Ή͠Ζ git ͕ͳ͍ͱෆ҆ʹͳΓ·͢ɻ • มߋ͕ه࿥͞Εͳ͍ͳΜͯɺා͍…ʂ

    • มߋ͕ه࿥͞Ε͍ͯΔͱ͍͏͜ͱ͸ɺ͍ͭͰ΋ͦͷه࿥ ʹ໭ΕΔͱ͍͏͜ͱͰ͢ɻ • ήʔϜͰ΋ॏཁͳηʔϒϙΠϯτͰ͢Ͷɻηʔϒ͠ͳ͍༐ ऀ΋ډΔ͔΋͠Ε·ͤΜ͕ɻ
  42. ! ! $ cat test.txt ↵ This is a pan.

    ! ! pan Ͱ͸ͳ͘ pen ͩͬͨ͜ͱʹؾ͕͖ͭ·ͨ͠ɻ
  43. ! ! $ cat test.txt ↵ This is a pan.

    ! ! ! $ cat test.txt ↵ This is a ΃Μ. pan Ͱ͸ͳ͘ pen ͩͬͨ͜ͱʹؾ͕͖ͭ·ͨ͠ɻ ௚ͯ͠Έ·͢ɻ
  44. ! ! $ cat test.txt ↵ This is a pan.

    ! ! ! $ cat test.txt ↵ This is a ΃Μ. ! ! ! ! $ git checkout test.txt ↵ $ cat test.txt ↵ This is a pan. pan Ͱ͸ͳ͘ pen ͩͬͨ͜ͱʹؾ͕͖ͭ·ͨ͠ɻ ௚ͯ͠Έ·͢ɻ ͪΌΜͱ௚ͨͭ͠΋Γ͕ɺͳΜ͔ؒҧ͍͑ͯ·ͨ͠ɻ ͍ͬͨΜ࠷ॳͷঢ়ଶʹ໭ͨ͘͠ͳΓ·ͨ͠ɻ
  45. ! ! $ cat test.txt ↵ This is a pen.

    ! ! ͜ΜͲ͸ͪΌΜͱ௚ͦ͏ͱࢥ͍·͢ɻ
  46. ! ! $ cat test.txt ↵ This is a pen.

    ! ! ! $ git add test.txt ↵ $ git commit -m “৯ཉશ։ͩͬͨͷͰमਖ਼͠·ͨ͠” ↵ [master 25ea33c] ৯ཉશ։ͩͬͨͷͰमਖ਼͠·ͨ͠ 1 file changed, 1 insertion(+), 1 deletion(-) ͜ΜͲ͸ͪΌΜͱ௚ͦ͏ͱࢥ͍·͢ɻ มߋΛه࿥͠·͢ɻϑΝΠϧͷ௥ՃͱҰॹͰ͢ɻ
  47. ! ! ! ! $ git log ↵ commit 25ea33ceace1a46c258d7119281164427ab0cebd

    Author: Keisuke SATO <riaf@me.com> Date: Fri May 30 20:44:03 2014 +0900 ! ৯ཉશ։ͩͬͨͷͰमਖ਼͠·ͨ͠ ! commit 026c173f1e8bc13a6fc0a7116792a32feb9dd31f Author: Keisuke SATO <riaf@me.com> Date: Fri May 30 20:28:02 2014 +0900 ! test.txt Λ௥Ճ͠·ͨ͠ ͜͜Ͱɺࠓ·Ͱ͜ͷϦϙδτϦͰͲͷΑ͏ͳมߋ͕ߦΘ Ε͍ͯͨͷ͔Λ֬ೝͨ͘͠ͳͬͨͱ͠·͢ɻ ίϛοτ͸ϋογϡ஋Ͱه࿥͞Ε͍ͯ·͢ɻ มߋΛಛఆ͢Δ ID ͷ༻ͳ΋ͷͰ͢ɻ ʮ25ea33c… ͷίϛοτʯ
  48. ! ! ! $ git show 25ea33ceace1a46c258d7119281164427ab0cebd ↵ commit 25ea33ceace1a46c258d7119281164427ab0cebd

    Author: Keisuke SATO <riaf@me.com> Date: Fri May 30 20:44:03 2014 +0900 ! ৯ཉશ։ͩͬͨͷͰमਖ਼͠·ͨ͠ ! diff --git a/test.txt b/test.txt index a938556..149bbea 100644 --- a/test.txt +++ b/test.txt @@ -1 +1 @@ -This is a pan. +This is a pen. ͯ͞ɺͦͷมߋ͸ԿΛߦͬͨͷͰ͠ΐ͏͔ɻ
  49. มߋΛه࿥͢Δͱ͍͏͜ͱ • ه࿥͞Εͨมߋ͸ɺޙ͔Β͔͞ͷ΅ͬͯ֬ೝ͢Δ͜ͱ ͕Ͱ͖·͢ɻ • ͓͔͠ͳ͜ͱ͕͓ͬͯ͜΋؆୯ʹಛఆͰ͖·͢ɻ • ͜ͷޡࣈɺ͍͔ͭΒͩʂʁ • લ͸େৎ෉ͩͬͨͷʹͳΜ͔͓͔͘͠ͳͬͨʂ

  50. ͜Ε·Ͱͷ͓͞Β͍ • ϦϙδτϦΛ࡞ͬͯ (git init) • ϑΝΠϧΛ௥Ճͯ͠ (git add) •

    มߋΛه࿥͢Δ (git commit) • ه࿥ͨ͠ϑΝΠϧʹஔ͖׵͑Δ (git checkout) • ޙ͔ΒมߋΛ֬ೝ͢Δ (git log / git show)
  51. ΈΜͳͰ࢖͏ Git

  52. 1ਓͰ࢖͏ͷ΋͍͍ΜͰ͕͢ • όʔδϣϯ؅ཧͷྗΛ΋ͬͱ׆༻͢Δʹ͸΍ͬͺΓෳ ਺ਓͰ͍Ζ͍Ζ͍͡Δͱ͖Ͱ͢ɻ • ͔͜͜Β͸࣮ࡍͷಈ࡞ͱ͍͏ΑΓ͸ɺ׆༻͢ΔΠϝʔ δΛϝΠϯʹෳ਺ਓͰ Git Λ࢖͏ͱ͖ͷ͓࿩Λ͠·͢ɻ •

    ࣮ࡍʹ΍Δ͜ͱ͸جຊతʹ͜Ε·Ͱग़͖ͯͨ͜ͱ͕΄ ͱΜͲͰ͢ɻ
  53. มߋͷྺ࢙ git init มߋ A มߋ B

  54. ϒϥϯν A B A’ B’ • git branch

  55. ϒϥϯνͷϚʔδ A B A’ B’ C (A’, B’) • git

    merge
  56. rebase A B A’ B’ • git rebase • Ͳͷίϛοτ͔Βϒϥϯ

    νΛ࡞੒ͨ͠ͷ͔ɺΛվ ͟Μ͢Δ
  57. rebase A B A’ B’ • git rebase • Ͳͷίϛοτ͔Βϒϥϯ

    νΛ࡞੒ͨ͠ͷ͔ɺΛվ ͟Μ͢Δ
  58. ಉ࣌ʹ͍ΖΜͳมߋ͕Ͱ͖ͯศརͳͷ͸ྑ͍͕ɺ Ͳ͏΍ͬͯڞ༗͢ΜͶΜɻ

  59. ϦϙδτϦΛڞ༗͢Δ

  60. ϦϙδτϦΛڞ༗͢Δ Pull Push

  61. ϦϙδτϦΛڞ༗͢Δ Pull Push

  62. ϦϙδτϦΛڞ༗͢Δ

  63. ϦϙδτϦΛڞ༗͢Δ

  64. Git ΛΠϯετʔϧ͍ͨ͠

  65. Mac OSX • σϑΥϧτͰೖͬͯΔΜ͡Όͳ͍Ͱ͔͢Ͷɻ • ΋͔ͨ͠͠Β Xcode ΛΠϯετʔϧ͢Δඞཁ͕͋Δ ͔΋ʁ •

    ΋͠ɺ৽͍͠όʔδϣϯΛೖΕ͍ͨͱ͔Ͱ͋Ε͹ɺ Homebrew Λ࢖͑͹؆୯ʹΠϯετʔϧͰ͖·͢ɻ • GitHub for Mac, SourceTree for Mac
  66. Linux • apt-get install git • ׬શʹσΟετϦϏϡʔγϣϯΛݶఆ͍ͯ͠Δ

  67. Windows • ެࣜʹ͸αϙʔτ͞Εͯ·ͤΜ • Git for Windows (msysGit) • SourceTree

    for Windows • GitHub for Windows
  68. ͓͞Β͍ • git • init, add, commit, checkout, log, show,

    pull, push, branch, rebase • GitHub • PullRequest ͸࠷ߴʹྑ͍ • ๺ւಓେ޷͖
  69. # shutdown -h now ↵ Broadcast message from root (pts/1)

    (Sat May 31 17:32:16 2014): The system is going down for reboot NOW!
  70. None