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

Next Level Git

Next Level Git

社内向けのGit勉強会で使ったスライドです。

Daiki Hirakata

December 11, 2017
Tweet

More Decks by Daiki Hirakata

Other Decks in Technology

Transcript

  1. Who am I ? • Daiki Hirakata • Software Engineer

    • Primary interests:
 DevOps, Docker, Kubernetes, Microservice Architecture, GCP @bigplants
  2. Agenda • Introduction • Briefly explanation of Git • Psychological

    barriers to using Git • Understanding “git flow” • What should we write commit log? • What should we do in this kind of situation? • OSS contribution is very easy!
  3. Why do I say Git Git Git? • Git͸VCSͷσϑΝΫτ͔ͩΒɻ •

    Git͸ΤϯδχΞಉ࢜ͷίϛϡχέʔγϣϯͦͷ΋ͷ ɻ • άϩʔόϧਓࡐʹͳΔͨΊʹӳޠΛ࿩ͤΔΑ͏ʹͳΔͷͱಉ͡Α͏ʹɺ ΤϯδχΞͳΒGit͘Β͍͸͢ΜͳΓѻ͑ΔΑ͏ʹͳ͓͖͍ͬͯͨɻ • ࠓ΍ϓϩμΫτͷߋ৽αΠΫϧͷதͰͳͯ͘͸ͳΒͳ͍ଘࡏɻ • GitHubͰࣾ಺ެ։͢Δ͜ͱʹΑͬͯɺΑΓΦʔϓϯʹͳΓιʔεͷԣల ։͕ग़དྷΔɺͭ·Γ͸ձࣾશମͷφϨοδ͕஝ੵ͞ΕΔɻͦͯ͠ίϥϘ Ϩʔγϣϯ͕ՄೳʹͳΔɻ
  4. Communication is very important! • ͲΜͳʹ༏ΕͨΤϯδχΞͰ΋ίϛϡχέʔγϣϯ͕͏·͘औΕͳ͍ͱ ࢒೦ͳ͜ͱʹͳΔ৔߹͕͋Δɻ • େମͷϓϩδΣΫτ͕ෳ਺ਓͰ։ൃ͍ͯ͠ΔɻੈͷதʹࢄΒ͹ͬͯΔ༏ ΕͨϥΠϒϥϦ΋ͨͬͨҰਓͰ͸։ൃͰ͖ͳ͍ɻͦ͜ʹ͸։ൃऀͷ૝͍

    ͕٧·͓ͬͯΓɺ૬ख΁ͷ഑ྀɺؾ഑Γ͕͋ͬͯॳΊͯ੒ཱ͢Δɻ • ίʔυΛಡΈ΍͘͢͢Δɺࠩ෼ΛΘ͔Γ΍͘͢͢Δࣄ͸ɺଞͷΤϯδχ ΞͱؔΘΔ্Ͱͱͯ΋ॏཁʹͳͬͯ͘Δɻ • ͢΂ͯͷ׆ಈ͸ίϛϡχέʔγϣϯΛҙࣝͨ݁͠ՌͰ͋Δ΂͖ɻ • ͦͷίϛϡχέʔγϣϯखஈʹGit͸͏͚ͬͯͭɻ
  5. git pullͷ͸ͳ͠ • git pullΛ͢Δͱɺ
 1. ϦϞʔτϒϥϯνΛμ΢ϯϩʔυ͠ɺϦϞʔτ௥੻ϒϥϯνΛߋ৽
 2. ϦϞʔτ௥੻ϒϥϯνΛϩʔΧϧϒϥϯνʹϚʔδ
 


    ͱ͍͏ྲྀΕʹͳΔɻ • git pull͸಺෦తʹ͸ɺ
 git fetch
 git merge origin <current_branch>
 
 Λ΍͍ͬͯΔɻ
  6. τϥϒϧΛආ͚Δҝʹ࠷௿ݶ΍ͬ ͓ͯ͘΂͖ઃఆ • վߦίʔυCRLFͷѻ͍ʹ͍ͭͯɻνΣοΫΞ΢τ࣌͸Կ΋ม׵ ͤͣίϛοτ࣌͸CRLF -> LF ʹม׵͢Δɻ
 
 git

    config --global core.autocrlf input
 
 ※ core.autocrlf ͸ϓϩδΣΫτʹΑΓ·͢ɻCRLFΛڐ༰͠ͳ͚ Ε͹͍͚ͳ͍γνϡΤʔγϣϯͷ৔߹͸ɺfalse౳Λࢦఆ͢Δɻ
 CRLFͷ஫ҙࣄ߲͸ɺҎԼͷهࣄ͕ࢀߟʹͳΓ·͢ɻ
 https://qiita.com/uggds/items/00a1974ec4f115616580
  7. Կ͔͋ͬͨΒඞͣ֬ೝ • ݱࡏͷεςʔλε֬ೝ
 git status • ϩάΛ֬ೝ
 git log •

    ϒϥϯνΛ֬ೝ
 git branch -a • ࠩ෼Λ֬ೝ
 git diff <commit>
  8. Why should we concern “git flow”? • ϓϩδΣΫτʹෳ਺։ൃऀɺෳ਺ͷ໾ׂͷਓ ͕͍Δͱ͖ʹঢ়ଶΛ೺Ѳ͠΍͘͢͢ΔͨΊ •

    ຊ൪؀ڥɺεςʔδ؀ڥͷࠩ෼Λೝࣝ͢Δͨ Ί • ༷ʑͳঢ়گʹରԠͰ͖ΔΑ͏ʹ͢ΔͨΊ
  9. develop branch • developϒϥϯν͸ৗʹଘࡏ ͢Δɻ • GitHubͰϒϥϯνΛอޢͰ͖ ·͢ɻ • developϒϥϯν͸ͲΜͲΜ

    ਐΜͰ͍͘ɻ • ఆظతʹdevelopϒϥϯνͷ ߋ৽͸featureϒϥϯνʹऔΓ ࠐΉɻ
  10. release branch • releaseϒϥϯν͸ϦϦʔε४උʹ࢖͏ɻ • ϦϦʔε४උʹೖͬͨΒreleaseϒϥϯν Λ࡞੒͢Δɻ • εςʔδϯά؀ڥʹσϓϩΠɻ •

    ϦϦʔεͷҝͷόάϑΟοΫεͳͲ͸ releaseϒϥϯν͔ΒϒϥϯνΛ࡞੒ͯ͠ Ϛʔδ͢Δɻ • releaseϒϥϯνͰमਖ਼ͨ͠΋ͷ͸ developϒϥϯνʹϚʔδ͢Δɻ • developϒϥϯν͸ৗʹ࠷৽ʹͳΔΑ͏ ʹ͢Δɻ
  11. tagͰΑ͘࢖͏ίϚϯυ • λάͷҰཡදࣔ
 git tag --list • λάͷ௥Ճ
 git tag

    -a <tag_name> -m <message> • λάͷৄࡉදࣔ
 git show <tag_name> • λάͷpush
 git push origin <tag_name>
  12. ઀಄ࣙΛ͚ͭͯमਖ਼ͷλΠϓΛ ໌֬ʹ͢Δ • Fix: όάमਖ਼ • Add: ৽نͷ(ϑΝΠϧ)ػೳ௥Ճ • Modify:

    όάͰ͸ͳ͍ػೳͷमਖ਼ • Clean: ϦϑΝΫλϦϯά౳ʹΑΔ੔ཧ • Remove: ࡟আ
  13. ͜Μͳ࣌Ͳ͏͢Δʁ • ؒҧͬͨͷͰίϛοτऔΓফ͍ͨ͠ɻ • PushऔΓফ͍ͨ͠ɻ • ϓϧϦΫͷϚʔδΛऔΓফ͍ͨ͠ɻ • औΓফͨ͠Ϛʔδ͔Β௥ՃͰ࡞ۀ͍ͨ͠ɻ •

    ࡞ۀதʹۓٸͷׂΓࠐΈ͕ೖΓɺҰ୴࡞ۀ಺༰Λίϛοτ͠ͳ͍Ͱୀආ͠ͱ͖ ͍ͨɻ • ҎલɺൿີͷΩʔΛpushͯ͠͠·ͬͨɻաڈͷྺ࢙͔ΒऔΓআ͖͍ͨɻ • ͱʹ͔͘ԿΒ͔ͷૢ࡞ΛऔΓফ͍ͨ͠ɻ(ϩʔΧϧͷΈ)
  14. PushऔΓফ͍ͨ͠ (ࣗ෼ͷϒϥϯ νલఏ) • git reset HEAD^ • git push

    -f
 ※ ڧ੍pushͷҝɺӬଓϒϥϯνͰ͸΍ͬͯ͸μϝɻઈରʂ
 ΍ͬͪΌͬͨ৔߹͸଎΍͔ʹϝϯόશһʹใࠂɻ
 ࡉ͔͍࿩͸͠·ͤΜ͕ɺ৔߹ʹΑͬͯ͸ଞͷਓ͕git pullͨ࣌͠ʹΤ ϥʔ͕ग़ΔͷͰ͔ͳΓ໽հͰ͢ɻ
 ˞ GitHubͰ͸͜ͷࣗମΛ๷͙ҝʹϒϥϯνΛอޢ͢Δࣄ͕Ͱ͖· ͢ɻ͜Ε͸΍͓ͬͯ͘΂͠ɻ

  15. ࡞ۀதʹۓٸͷׂΓࠐΈ͕ೖΓɺҰ୴࡞ۀ ಺༰Λίϛοτ͠ͳ͍Ͱୀආ͠ͱ͖͍ͨ • ࡞ۀΛୀආ͢Δ
 git stash • ్தͷ࡞ۀͷҰཡදࣔ
 git stash

    list • ్தͷ࡞ۀΛ෮ݩ
 git stash pop <stash>
 
 ࢀߟࢿྉɿhttps://qiita.com/fukajun/items/ 41288806e4733cb9c342
  16. ҎલʹൿີͷΩʔΛpushͯ͠͠ ·ͬͨɻաڈͷྺ࢙͔Βຣফ͍ͨ͠ • git filter-branch -f --index-filter 'git rm --

    cached --ignore-unmatch FILENAME' -- prune-empty --tag-name-filter cat -- --all
 
 git push -f
 
 ࢀߟهࣄ: https://qiita.com/iorionda/items/ 88d6329af997b5c5e748
  17. ύεϫʔυ΍Ωʔͷ৘ใͳͲ͸ gitignore͢Δ • .gitignoreϑΝΠϧΛ࡞੒ • ແࢹ͢ΔϑΝΠϧ΍σΟϨΫτϦͷύεΛهࡌ • ϑΝΠϧͷத਎
 .env
 .idea


    ͳͲ • جຊతʹɺ༨ܭͳϑΝΠϧ͸gitignoreͰແࢹ͢Δ΂͠ɻ
 ྫ͑͹ɺIDEͷઃఆϑΝΠϧͳͲɻ͠ΐͬͪΎ͏ࠩ෼্͕͕ͬͯ͘Δ͕ɺݸਓ༻ͷ ઃఆϑΝΠϧͳͲɻ

  18. ͱʹ͔͘ԿΒ͔ͷૢ࡞ΛऔΓফ ͍ͨ͠ɻ(ϩʔΧϧͷΈ) • ྫ͑͹ɺؒҧͬͯgit reset --hard HEAD^^ͱ͔΍ͬͯ͠·ͬͨɻͳ Ͳ • gitͷૢ࡞ཤྺΛදࣔ


    git reflog • ର৅ͷૢ࡞ͷ௚ޙͷঢ়ଶʹ໭͢
 git reset --hard HEAD@{<number>}
 
 ※ ͪͳΈʹ͜ͷૢ࡞ࣗମΛऔΓফ͢ࣄ΋Ͱ͖·͢ɻ
 ࢀߟࢿྉɿhttps://qiita.com/yaotti/items/e37c707938847aee671b
  19. औΓফͨ͠Ϛʔδ͔Β௥ՃͰ࡞ ۀ͍ͨ͠ • ϦόʔτͷϦόʔτΛ͢Δ
 ʁʁʁͬͯͳͬͨʁ • ྲྀΕΛઆ໌͢Δͱɺ
 ࠩ෼A (master)
 ↓


    ࠩ෼B (master)
 ↓
 ࠩ෼Bͷଧফ͠(ࠩ෼Aͱࠩ෼͕ͳ͍ঢ়ଶ) (master)
 ↓
 ࠩ෼Bͷଧফ͠ͷଧফ͠ (revert-revert-branch)
 ↓
 ௥Ճमਖ਼ (revert-revert-branch)
 ↓
 վΊͯϚʔδ (master)
  20. ίϯτϦϏϡʔτ·ͰͷྲྀΕ 1. ར༻͍ͯ͠ΔϥΠϒϥϦʹόάΛൃݟɻ 2. GitHubͰର৅ͷϥΠϒϥϦͷϦϙδτϦΛϑΥʔΫɻ 3. ϑΥʔΫͨ͠ϦϙδτϦʹύεΛ੾Γସ͑Δ(package.json, composer.json, Gemfile౳) 4.

    ϒϥϯν੾ͬͯमਖ਼ɺpushɻ 5. ϓϧϦΫΛൃߦɻ˞֤ϦϙδτϦͷίϯτϦϏϡʔγϣϯΨΠυΛཁ֬ೝ 6. ϓϧϦΫ͕Ϛʔδ͞Εɺόʔδϣϯ͕ߋ৽͞ΕͨΒɺύοέʔδͷύεΛߋ৽ (package.json౳)ɻ 7. Ҏ্
  21. ϑΥʔΫͷϕετϓϥΫςΟε Railsͷྫ 1. GitHub্ͰϑΥʔΫ
 rails/railsΛϑΥʔΫ͢Δͱɺ <your_account>/rails͕࡞੒͞ΕΔ 2. git clone [email protected]:<your_account>/rails.git

    3. ຊՈͷߋ৽Λ௥੻Ͱ͖ΔΑ͏ʹ͢Δ
 git remote add upstream [email protected]:rails/rails.git 4. ϒϥϯνΛ੾ͬͯ࡞ۀ͠push͢Δ
 ˞ masterͰ࡞ۀ͢Δࣄ΋Ͱ͖·͕͢ɺຊՈͷߋ৽Λ௥ै͢ΔҝʹmasterϒϥϯνͰ͸࡞ۀ͠ͳ͍Α͏ʹ͠·͢ 5. ϓϧϦΫΤετΛൃߦ
 Ϛʔδઌͷϒϥϯν΍ϓϧϦΫͷ಺༰ʹ͍ͭͯ͸ɺ֤ϦϙδτϦͷίϯτϦϏϡʔγϣϯΨΠυΛཁ֬ೝ
  22. ͜͜Ͱoriginͷখ࿩ • Α͘ొ৔͢ΔoriginͬͯԿͳͷʁ
 ࣮͸ϦϙδτϦURLͷͨͩͷΤΠϦΞε • git config --list --local ͯ͠ΈΔ


    [email protected]:<your_accout>/ rails.git • git pull origin masterͷ୅ΘΓʹɺ
 git pull [email protected]:<your_accout>/rails.git master
 Ͱ΋ಉ݁͡ՌʹͳΔ