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

入門書には載ってない Git & GitHub Tips

入門書には載ってない Git & GitHub Tips

第一回 GitHub Kaigi で発表した資料です。

Yuku TAKAHASHI

June 01, 2014
Tweet

More Decks by Yuku TAKAHASHI

Other Decks in Programming

Transcript

  1. ߴڮါٱ ͔ͨ͸͠ Ώ͏͘ yuku_t yuku-t GitHub
 ͱͷೃΕॳΊ 2013/02 GitHub૑ۀऀ&CIOͱژ౎؍ޫΛָ͠Ή 2013/07

    GitHubͷϓϥΠϕʔτϧʔϜͰେϦʔά؍ઓ͢Δ 2014/06 GitHub KaigiͰ࿩͢ CTO @ Increments
  2. ίϯϑϦΫτൃੜ࣌ͷ໰୊ % git merge master 7 HEAD master <<<<<<< HEAD

    topic branch ======= master >>>>>>> master !!! ίϯϑϦΫτ͢ΔͱHEADͱϚʔδର৅͕ॻ͖ग़͞ΕΔ ➡΋ͱͷঢ়ଶ͕෼͔Βͳͯ͘ࠔΔ͜ͱ͕͋Δ
  3. ڞ௨ͷ૆ઌͷ৘ใΛॻ͖ग़͢ 8 http://qiita.com/hchbaw/items/1191c2627307a4673b1b <<<<<<< HEAD topic branch ||||||| merged common

    ancestors original ======= master >>>>>>> master HEAD master • ڞ௨ͷ૆ઌͷঢ়ଶ΋ॻ͖ग़͞ΕΔΑ͏ʹͳΔ - ௨ৗͷΤσΟλͰίϯϑϦΫτղফͯ͠Δਓʹ͸ศར - mergetoolΛ࢖͍ͬͯΔਓʹ͸ಛʹϝϦοτ͸ͳ͍ % git config --global merge.conflictstyle diff3
  4. git stash save / pop • indexͷ৘ใ͕ܽམ ➡ file͕શମ͕unstagedʹ 10

    • track͞Ε͍ͯͳ͍ϑΝΠϧ͸
 ࢒͞ΕΔ ➡ readme͕࢒͍ͬͯΔ • indexͨ͠΋ͷ΋stash͞ΕΔ ➡ fileʹaddͨ͠෦෼΋stash ‣ -q,--quiet ͸ඪ४ग़ྗΛ཈੍͢Δ
  5. --all --include-untracked --keep-index -u, --include-untracked • track͞Ε͍ͯͳ͍
 ϑΝΠϧ΋stash͢Δ ➡ readme΋stash͞ΕΔ

    -a, --all • ignoredͳϑΝΠϧ΋શ෦ 11 -k, --keep-index • index treeΛͦͷ··࢒͢ ➡ fileͷadd͞ΕͨՕॴ͕ ࢒͍ͬͯΔ http://qiita.com/ton1517/items/9888a78f6b063e748558
  6. git stash pop --index --index • working tree͚ͩͰͳ͘index tree΋෮ݩ͢Δ ➡

    fileͷadd͞Εͨঢ়ଶ͕෮ݩ͞Ε͍ͯΔ 12 http://qiita.com/ton1517/items/9888a78f6b063e748558
  7. શ͘৽͍͠working directory͕ཉ͍͠ • git stash -uͰશͯstashͰ͖Δ͚Ͳɺ1͔Β
 ৽͍͠working directory͕ཉ͘͠ͳΔͱ͖΋͋Δ ! •

    .gitσΟϨΫτϦͷத਎͕γϯϘϦοΫϦϯΫͰ
 ͭͳ͕͍ͬͯΔͷͰશͯͷΦϒδΣΫτ͕ಉظ͢Δ ➡ stash΋ಉظ͢ΔͷͰnew-workdirͰॻ͍ͨίʔυΛ stash popͰ؆୯ʹऔΓࠐΊΔ ʦ༨ஊʧgit-core/contribʹ͸ศརͳεΫϦϓτ͕৭ʑ ๏ ಛʹdiff-highlight࢖ͬͯͳ͍ਓ͸ਓੜଛͯ͠Δ 13 http://qiita.com/yuya_presto/items/dcebbebc6b3d9cf6f542 % ln -s /path/to/git-core/contrib/workdir/git-new-workdir ~/bin % git-new-workdir . ../new-workdir % cd ../new-workdir # ৽͍͠working directory
  8. ۭനͷҧ͍͚ͩͷߦΛআ͍ͯadd ✓ ۭനҧ͍͚ͩͷίϛοτ͕ผͩͱͪΐͬͱخ͍͠ -w, --ignore-all-space • ࠩ෼ܭࢉ࣌ʹۭനจࣈΛແࢹ͢Δɻ ! --cached •

    working treeΛ৮ΒͣʹindexͷΈߋ৽͢Δ 15 % git diff -w % git apply --cached < patch % git diff -w | git apply --cached http://qiita.com/yuya_presto/items/dcebbebc6b3d9cf6f542 ※ ۭനͷมߋͱଞͷมߋ͕ۙ͘ʹ͋ΔͱίϯϑϦΫτ͕ൃੜͯ͠࢖͑·ͤΜ
  9. ͦ΋ͦ΋git fsckͱ͸ʁ • gitͷ࣮ମ͸άϥϑ - ݸʑͷϊʔυ͸sha1஋Λ͕࣋ͭ… • ࢖͏ίϛοτʹ͸໊લ(ref)Λ͚ͭΔ - HEAD,

    branch, tagͳͲͳͲ • refs͔ΒḷΕͳ͍΋ͷ͸ΰϛ • git fsck͸ΰϛΛ൑ఆˍྻڍ͢Δ 17 ref % git fsck dangling commit ͷsha1 ※ ࣮ࡍʹΰϛΛফ͢ͷ͸git prune
  10. addͯ͠commitͤͣʹreset --hardͨ͠ • ΰϛ൑ఆ͞ΕͨΦϒδΣΫτΛॻ͖ग़͢ - .git/lost-found/commit/ - .git/lost-found/other/ # ͬͪ͜

    • ॻ͖ͩ͞ΕͨϑΝΠϧΛgrepͯ͠໨౰ͯͷϑΝΠϧ Λ୳ͯ͠෮ݩ͢Δ 18 http://qiita.com/yoshiori/items/6da867aa6871be694996 % git fsck --lost-found
  11. ޡͬͯgit stash clearͨ͠ • git fsck͕ग़ྗ͢ΔϚʔδίϛοτͰsubjectʹ WIPΛؚΉ΋ͷΛྻڍ͢Δ ! • ໨౰ͯͷsha1͕෼͔ͬͨΒgit

    stash applyͰ
 ద༻Ͱ͖Δ 19 % git fsck | grep commit | cut -d' ' -f3 | xargs git log --merges --no-walk --grep=WIP % git stash apply <stash-sha1>
  12. hub • CLI͔ΒGitHubΛૢ࡞͢ΔͨΊͷπʔϧ ! - ݱࡏͷϦϙδτϦΛϒϥ΢βͰ։͘ ! - current branchΛϓϧϦΫΤετʹ͢Δ

    21 http://qiita.com/yaotti/items/a4a7f3f9a38d7d3415e3 % hub pull-request % hub browse
  13. ۭͷPullRequestΛ࡞ͬͯ࡞ۀΛՄࢹԽ • GitHub͸ϑΝΠϧมߋແ͠ͷPullRequestΛ࡞ΕΔ - ࡞ۀΛ࢝ΊͨΒͱΓ͋͑ͣ࡞ͬͯcommit͢Δ౓ʹ push͍ͯ͘͠ - આ໌ʹλεΫΛྻڍ - पΓ͕ঢ়گΛ೺Ѳ͠΍͍͢

    22 http://qiita.com/a-suenami/items/129e09f8550f31e4c2da % git checkout -b projectx % git commit --allow-empty -m ProjectX % git push % hub pull-request # or WebUIͰPR࡞੒
  14. git rebase -i --autosquash • ؾܰͳpush -fΛې͍ͯ͡Δͱɺඍௐ੔ͷͨΊͷ
 ࠣࡉͳίϛοτ͕ͨ͘͞Μ࡞ΒΕͯϩά͕෼͔Γʹ ͘͘ͳΔ -

    “squash! squashର৅ίϛοτͷίϝϯτ”
 ͱίϛοτ͢Δͱinteractive rebase࣌ʹࣗಈతʹ ͍͍ײ͡ʹͯ͘͠ΕΔ • શͯͷ࡞ۀ͕׬ྃͨ͠ஈ֊Ͱautosquashͯ͠
 ʢपΓͱҙࢥૄ௨ΛऔΓͭͭʣpush -f 24 http://qiita.com/kyanro@github/items/818012c1b1827ed48277
  15. autosquashΛָʹ͢Δ • ৗʹautosquash͢Δ ‣ git rebase࣌ʹࣗಈͰ--autosquashΦϓγϣϯ Λ௥Ճ͢ΔΑ͏ʹͳΔ ‣ --autosquash͸--interactive࣌ͷΈӨڹ͢ ΔͷͰͱΓ͋͑ͣtrueͰ΋ಛʹ໰୊ͳ͍ʢ͸ͣʣ

    • ίϛοτϝοηʔδͷࣗಈੜ੒ ‣ autosquash༻ͷϝοηʔδΛࣗಈੜ੒ͯ͘͠ΕΔ ‣ --fixup ΦϓγϣϯͰfixup΋Ͱ͖Δ 25 http://qiita.com/kyanro@github/items/818012c1b1827ed48277 % git config --global rebase.autosquash true % git commit --squash <commit>
  16. ϒϥϯν໊Λௐ΂Δͷ͕໘౗͍͘͞໰୊ • ϨϏϡʔΛґཔ͞ΕͨϒϥϯνΛϩʔΧϧʹ checkout͢Δͷʹ໊લΛௐ΂Δͷ͕໘౗ ! • GitHub APIͰϓϧϦΫΤετΛऔಘͯ͠ߜΓࠐΉ 26 %

    prfetch|percol|cut -f3|xargs git checkout http://qiita.com/yuku_t/items/f53a9d3ea92614b0927d ͜͜Λؤுͬͯίϐϖͯͨ͠….
  17. alias.branch-root • τϐοΫϒϥϯνͷࠜຊͷsha1͕෼͔ΔͱҰ࿈ͷί ϛοτʹߜΓࠐΜΉ͜ͱ͕Ͱ͖Δ ! ‣ τϐοΫϒϥϯνͷൣғ಺Ͱgit log ! ‣

    τϐοΫϒϥϯν಺Ͱinteractive rebase 27 [alias] branch-root = merge-base master HEAD % git log $(git branch-root)..HEAD % git rebase -i $(git branch-root) http://qiita.com/yuku_t/items/5cf770157380952e9476
  18. ϨϏϡʔ͸ϩʔΧϧͰ • ϨϏϡʔ࣌͸৭ʑͳ৘ใΛࢀর͢ΔͨΊɺGitHubͷ Web UIͩͱ஗͗͢ΔʢGH:E͸Ͳ͏͔ͩ஌Γ·ͤΜʣ ✓ ݸਓతʹ͸tigਪ͠ 28 [alias] review

    = !tig --reverse -w $(git branch-root)..HEAD [tig “bind”] main = B @hub browse -- commit/%(commit) diff = B @hub browse -- commit/%(commit) tree = B @hub browse -- tree/%(commit)/%(directory) blob = B @hub browse -- tree/%(commit)/%(file) - ϨϏϡʔର৅ΛtigͰಡΈࠐΉalias ‣ --reverse Λ͚ͭΔͱGitHubͷUIͱฒͼํ͕ಉ͡ʹͳͬͯخ͍͠ - ίϝϯτ͸WebUIͰߦ͏͔͠ͳ͍ͷͰ֘౰ϖʔδ΁Ҡಈ͢ΔΩʔόΠϯυ͸ඞਢ