実務で使えるGitコマンド

 実務で使えるGitコマンド

ペチオブで発表した資料です。

今回はGitコマンドにフォーカスを当てて、「実務で使えるコマンド」ということで紹介します。

2人で作成したので少し長めの資料となっていますが、参考にしていただけたら幸いです。

4cd33cc6c5b13279f74efffc64ac2d27?s=128

yukimasa ikeda

October 24, 2019
Tweet

Transcript

  1. ࣮຿Ͱ࢖͑ΔGitίϚϯυ ϖνΦϒ 2019/10/24 akito/yukimasa

  2. ࣮຿ܦݧ6ϲ݄໨ͷGitൃදձ

  3. ࣗݾ঺հ • ໊લɹɿ ஑ా ༗ًকʢϢΩϚαɺϢΩʣ • ॴଐɹɿ גࣜձࣾϛϥΠτσβΠϯ • ݴޠɹɿ

    Ruby (Rails) / iOSʢswiftʣ/ Androidʢjavaʣ • ࣮຿ɹɿ 6ϲ݄໨ • લ৬ɹɿ ધഫ৐ધһʢλάϘʔτʣ • ग़਎ɹɿ ਆಸ઒ݝԣ඿ࢢ • Twitterɿ @yyykms123
  4. ࣗݾ঺հ • ໊લɹɿ ҝ౻ ΞΩτ • ॴଐɹɿ גࣜձࣾϛϥΠτσβΠϯ • ݴޠɹɿ

    Ruby (Rails) / iOSʢswiftʣ/ Androidʢjavaʣ • ࣮຿ɹɿ 6ϲ݄໨ • લ৬ɹɿ αϙʔτσεΫ • ग़਎ɹɿ ઍ༿ݝחέ୩ࢢ • Twitterɿ @akito_tameto • झຯ ɿ ےτϨ͕޷͖Ͱ͢ὑ
  5. ࠓ೔࿩͢͜ͱ 1. ࣮຿ͰΑ͘࢖͏ίϚϯυ 2. ࣮຿Ͱͨ·ʹ࢖͏ίϚϯυ 3. ͓·͚

  6. 1. ࣮຿ͰΑ͘࢖͏ίϚϯυ

  7. 1. ࣮຿ͰΑ͘࢖͏ίϚϯυ 7. git diff 8. git add 9. git

    commit 10. git log 11. git push 12. git reset 1. git pull 2. git fetch 3. git merge 4. git checkout 5. git branch 6. git status
  8. ·ͣ͸ɺͪ͜Βͷ3ͭʂʂ 7. git diff 8. git add 9. git commit

    10. git log 11. git push 12. git reset 1. git pull 2. git fetch 3. git merge 4. git checkout 5. git branch 6. git status
  9. ͦͷલʹɺલఏͱͯ͠ʂʂ • GithubͳͲͷ΢ΣϒαʔϏεΛར༻ͯ͠ιʔείʔ υΛ೚ҙͷσΟϨΫτϦͰ git clone Λ͠·͢ɻ • ࣍ʹɺΫϩʔϯΛͨ͠σΟϨΫτϦͰ git

    init Λߦ ͏͜ͱͰGitίϚϯυΛར༻͢Δࣄ͕Ͱ͖·͢ɻ
  10. ͦͯ͠ɺ3ͭͷྖҬ͕͋Γ·͢∑ ƅЧƅ)

  11. αʔόʔଆ ϩʔΧϧଆ ϦϞʔτ ϦϙδτϦ ϩʔΧϧ ϦϙδτϦ ϫʔΩϯά πϦʔ

  12. git pullͱ͸ʁ

  13. git pull • ϦϞʔτϦϙδτϦͷมߋཤྺΛϫʔΩϯάπϦʔ ΁μ΢ϯϩʔυͰ͖Δ • fetch͔Βmerge·ͰΛߦͳ͍ͬͯΔ $ git pull

    origin master
  14. αʔόʔଆ ϩʔΧϧଆ ϦϞʔτ ϦϙδτϦ ϩʔΧϧ ϦϙδτϦ ϫʔΩϯά πϦʔ pull

  15. git fetch • ϦϞʔτϦϙδτϦͷมߋཤྺΛϩʔΧϧϦϙδτϦ ΁μ΢ϯϩʔυͰ͖Δ $ git fetch origin •

    Φϓγϣϯ-pͰ࡟আ͞ΕͨϒϥϯνͳͲ΋൓өͰ͖Δ
  16. αʔόʔଆ ϩʔΧϧଆ ϦϞʔτ ϦϙδτϦ ϩʔΧϧ ϦϙδτϦ ϫʔΩϯά πϦʔ pull fetch

  17. git merge • ϩʔΧϧϦϙδτϦͷϒϥϯνΛϫʔΩϯάπϦʔͷ ϒϥϯνΛ݁߹Ͱ͖Δ $ git merge master

  18. αʔόʔଆ ϩʔΧϧଆ ϦϞʔτ ϦϙδτϦ ϩʔΧϧ ϦϙδτϦ ϫʔΩϯά πϦʔ pull fetch

    merge
  19. ݁ՌpullΛ࢖͑͹͍͍ͷʁ

  20. fetchͱmergeΛҰॹʹߦ͍ͬͯΔ ͱ͍͏ͷΛཧղͯ͠ɺ໰୊ͳ͚Ε͹ େৎ෉ʂʂ

  21. ࣍͸ɺͪ͜Βʂʂ 7. git diff 8. git add 9. git commit

    10. git log 11. git push 12. git reset 1. git pull 2. git fetch 3. git merge 4. git checkout 5. git branch 6. git status
  22. git checkout • Φϓγϣϯ-bΛ͚ͭΔ͜ͱͰݱࡏͷϒϥϯν͔Β ৽͍͠ϒϥϯνΛ࡞੒͢Δ͜ͱ͕Ͱ͖Δ • ฤूͨ͠ϑΝΠϧΛϦηοτ͢Δࣄ΋Ͱ͖Δ • ผϒϥϯνʹ੾Γସ͑Δ͜ͱ΋Ͱ͖Δ $

    $ git checkout -b B
  23. " " $ $ git checkout A # ݱࡏͷϒϥϯν "

    " # ݱࡏͷϒϥϯν
  24. git branch • ϒϥϯνͷҰཡΛ֬ೝͰ͖Δ $ $ git branch -D ࡟আ͢Δϒϥϯν໊

    • ݱࡏͲͷϒϥϯν͔Λ֬ೝͰ͖Δ • Φϓγϣϯ-DͰϒϥϯνΛ࡟আͰ͖Δ
  25. ࣍͸ɺͪ͜Βʂʂ 7. git diff 8. git add 9. git commit

    10. git log 11. git push 12. git reset 1. git pull 2. git fetch 3. git merge 4. git checkout 5. git branch 6. git status
  26. git status • ϑΝΠϧमਖ਼ɺ௥ՃͳͲݱࡏͷঢ়گΛ֬ೝͰ͖Δ • جຊΦϓγϣϯΛ࢖༻͢Δ͜ͱ͸ͳ͍

  27. git diff • मਖ਼ͨ͠ϑΝΠϧͷৄࡉΛ֬ೝͰ͖Δ • ϑΝΠϧ໊Λࢦఆ͢ΔࣄͰͦͷϑΝΠϧͷΈͷৄࡉΛ ֬ೝͰ͖·͢

  28. ࣍͸ɺͪ͜Βʂʂ 7. git diff 8. git add 9. git commit

    10. git log 11. git push 12. git reset 1. git pull 2. git fetch 3. git merge 4. git checkout 5. git branch 6. git status
  29. git add • मਖ਼ɺ௥Ճͨ͠ϑΝΠϧΛεςʔδϯάΤϦΞ΁ ௥ՃͰ͖Δ $ $ git add .

    • ΦϓγϣϯͰ.(υοτ)Λར༻͢ΔͱશͯΛબ୒Ͱ͖Δ • ϑΝΠϧΛࢦఆ͢Δࣄ΋Ͱ͖Δ • ଞͷΦϓγϣϯ͸͓·͚Ͱ঺հ͠·͢ὑ
  30. ͦͷલʹεςʔδϯάΤϦΞͱ͸ʁ • εςʔδϯάΤϦΞͱ͸ίϛοτ͢ΔલʹҰ࣌తʹ อଘΛ͓ͯ͘͜͠ͱ͕Ͱ͖Δ

  31. αʔόʔଆ ϩʔΧϧଆ ϦϞʔτ ϦϙδτϦ ϩʔΧϧ ϦϙδτϦ ϫʔΩϯά πϦʔ εςʔδϯάΤϦΞ add

  32. git commit • ϦϙδτϦʹ൓ө͢Δʹ͸ొ࿥(ه࿥)Λ͠·͢ $ $ git commit -m ‘ίϛοτϝοηʔδ’

    • Φϓγϣϯ-mͰvimͳͲΛ։͔ͣʹίϛοτϝοηʔδΛ ೖྗͰ͖·͢
  33. αʔόʔଆ ϩʔΧϧଆ ϦϞʔτ ϦϙδτϦ ϩʔΧϧ ϦϙδτϦ ϫʔΩϯά πϦʔ εςʔδϯάΤϦΞ add

    commit
  34. ࣍͸ɺͪ͜Βʂʂ 7. git diff 8. git add 9. git commit

    10. git log 11. git push 12. git reset 1. git pull 2. git fetch 3. git merge 4. git checkout 5. git branch 6. git status
  35. git log • ίϛοτཤྺΛ֬ೝ͢Δࣄ͕Ͱ͖Δ • Φϓγϣϯ-਺ࣈͰཤྺΛߜΔࣄ͕Ͱ͖Δ • Φϓγϣϯ-onelineͰίϛοτཤྺΛ1ߦͮͭදࣔ • Φϓγϣϯ-graphͰάϥϑͰදࣔͰ͖Δ

  36. ࣍͸ɺͪ͜Βʂʂ 7. git diff 8. git add 9. git commit

    10. git log 11. git push 12. git reset 1. git pull 2. git fetch 3. git merge 4. git checkout 5. git branch 6. git status
  37. git push • ϩʔΧϧͰมߋͨ͠಺༰ϦϞʔτ΁ͱ൓өͰ͖Δ $ $ git push origin master

  38. αʔόʔଆ ϩʔΧϧଆ ϦϞʔτ ϦϙδτϦ ϩʔΧϧ ϦϙδτϦ ϫʔΩϯά πϦʔ εςʔδϯάΤϦΞ add

    commit push
  39. ࠷ޙʹͪ͜Βʂʂ 7. git diff 8. git add 9. git commit

    10. git log 11. git push 12. git reset 1. git pull 2. git fetch 3. git merge 4. git checkout 5. git branch 6. git status
  40. git reset • मਖ਼ɺaddɺcommitΛϦηοτ͢Δࣄ͕Ͱ͖Δ • Φϓγϣϯ--mixedͰίϛοτɺaddঢ়ଶΛ ϦηοτͰ͖·͢ • Φϓγϣϯ--softͰίϛοτͷঢ়ଶΛϦηοτ •

    Φϓγϣϯ--hardϑΝΠϧɺίϛοτɺaddঢ়ଶ ΛϦηοτͰ͖Δ
  41. ਤΛ·ͱΊΔͱ

  42. αʔόʔଆ ϩʔΧϧଆ ϦϞʔτ ϦϙδτϦ ϩʔΧϧ ϦϙδτϦ ϫʔΩϯά πϦʔ εςʔδϯάΤϦΞ add

    commit push pull fetch merge
  43. 2. ࣮຿Ͱͨ·ʹ࢖͏ίϚϯυ

  44. 2. ࣮຿Ͱͨ·ʹ࢖͏ίϚϯυ • git rebase • git cherry-pick • git

    stash • git revert
  45. 2. ࣮຿Ͱͨ·ʹ࢖͏ίϚϯυ • git rebase • git cherry-pick • git

    stash • git revert
  46. git rebase ͬͯԿʁ

  47. git rebase • ϒϥϯνΛ౷߹͢Δ • ίϛοτཤྺΛݟ΍͘͢͢Δ • ίϛοτͷॱ൪ΛೖΕସ͑Δ • ෳ਺ͷίϛοτΛ·ͱΊΔ

    • աڈͷίϛοτϝοηʔδΛมߋ͢Δ • 1౓ίϛοτͨ͠಺༰Λฤू͢Δ
  48. mergeͱrebaseͷҧ͍ͬͯʁ

  49. merge $ $ git checkout master $ $ git merge

    topic UPQJD NBTUFS
  50. NFSHFίϛοτ merge NBTUFS UPQJD $ $ git checkout master $

    $ git merge topic UPQJD NBTUFS
  51. rebase UPQJD NBTUFS $ $ git checkout topic $ $

    git rebase master
  52. rebase UPQJD NBTUFS $ $ git checkout topic $ $

    git rebase master UPQJD NBTUFS
  53. rebase UPQJD NBTUFS $ $ git checkout master $ $

    git merge topic $ NBTUFS UPQJD NBTUFS fast forward
  54. rebase UPQJD NBTUFS $ $ git checkout master $ $

    git merge topic $ NBTUFS NBTUFS UPQJD NFSHFίϛοτ non fast forward
  55. ϑΝετϑΥϫʔυͬͯʁ

  56. ϑΝετϑΥϫʔυ • merge͢ΔϒϥϯνͷHEADʹͦͷ··Ҡಈ ͢ΔʢϚʔδίϛοτΛ࡞Βͳ͍ʣ • ෼ذޙͰ྆ํͷϒϥϯνʹίϛοτ͕͋Δͱ ϑΝετϑΥϫʔυ͸Ͱ͖ͳ͍

  57. fast-forward UPQJD NBTUFS

  58. fast-forward UPQJD NBTUFS NBTUFS UPQJD

  59. non fast-forward UPQJD NBTUFS

  60. non fast-forward UPQJD NBTUFS NBTUFS UPQJD

  61. ϒϥϯν͕෼ذ͍ͯͨ͠Β UPQJD NBTUFS non fast-forwardͰϚʔδͱͳΔ

  62. rebaseͬͯԿΛ΍ͬͯΔͷʁ

  63. rebaseͷಈ͖ 1. topicͷίϛοτΛҰ࣌తʹอଘ 2. masterʹϦηοτ(git reset --hard master) 3. Ұ࣌తʹอଘͨ͠ίϛοτΛҰͭͣͭॱ൪ʹ

    ద༻͢Δ ※ίϛοτID͕มΘΔ
  64. rebaseͷಈ͖ UPQJD NBTUFS

  65. rebaseͷಈ͖ UPQJD NBTUFS Ұ࣌อଘ

  66. rebaseͷಈ͖ NBTUFS Ұ࣌อଘ UPQJD git reset --hard master

  67. rebaseͷಈ͖ NBTUFS Ұ࣌อଘ UPQJD ҰͭͣͭదԠ

  68. rebaseͷಈ͖ NBTUFS Ұ࣌อଘ UPQJD ҰͭͣͭదԠ

  69. rebaseͷ஫ҙ఺ ϦϞʔτʹpushͯ͋͠Δίϛοτ͸ rebase͠ͳ͍ʂʂ → ෳ਺ਓͰ࡞ۀ͢Δ৔߹ʹଞͷਓ͕pushͰ͖ͳ͘ ͳͬͯ͠·͏ɻ طʹϦϞʔτʹpush͞Ε͍ͯΔϒϥϯνΛrebase͠ ͯɺ͞ΒʹͦΕΛpush͢ΔͱίϛοτID͕มΘΔɻ

  70. ίϯϑϦΫτ͕ى͖ͨΒʁ

  71. ίϯϑϦΫτ $ $ git rebase master topic First, rewinding head

    to replay your work on top of it... Applying: new topic Using index info to reconstruct a base tree... M test.txt Falling back to patching base and 3-way merge... Auto-merging test.txt CONFLICT (content): Merge conflict in test.txt error: Failed to merge in the changes. Patch failed at 0001 new topic hint: Use 'git am --show-current-patch' to see the failed patch Resolve all conflicts manually, mark them as resolved with "git add/rm <conflicted_files>", then run "git rebase --continue". You can instead skip this commit: run "git rebase --skip". To abort and get back to the state before "git rebase", run "git rebase --abort".
  72. 3ͭͷબ୒ࢶ • git rebase --continue ίϯϑϦΫτΛղফ͔ͯ͠Βଓ͚Δ৔߹ • git rebase --skip

    ίϛοτΛඈ͹͢৔߹ • git rebase --abort rebaseΛதஅ͢Δ৔߹
  73. mergeͱrebaseͷҧ͍Λ ·ͱΊΔͱ

  74. rebase merge • ϒϥϯνͷཤྺ͕มߋ͞Εͳ͍ • Ϛʔδίϛοτ͕࡞੒͞ΕΔ • ن໛͕େ͖͘ͳΔͱཤྺ͕ෳࡶʹͳΔ • ཤྺ͕γϯϓϧʹͳΓݟ΍͘͢ͳΔ

    • ίϛοτID͕มߋ͞ΕΔͷͰ஫ҙ͕ඞཁ • ίϯϑϦΫτ࣌ͷରԠ͕ෳࡶ
  75. Ͳ͏΍ͬͯ࢖͍෼͚Δͷʁ

  76. ։ൃνʔϜͷӡ༻ʹΑΔʂ

  77. ϓϩδΣΫτϝϯόʔ͕Gitʹ ׳Ε͍ͯͳ͍৔߹

  78. ཤྺ͕࢒ͬͯϦΧόϦʔ͠΍͍͔͢Βɺ mergeͷํ͕͍͍͔΋ʂ

  79. ͱ͍͏͜ͱͰɺ ࠓ·Ͱͷ͕͜ͷ2ͭͷઆ໌ʂ

  80. git rebase • ϒϥϯνΛ౷߹͢Δ • ίϛοτཤྺΛݟ΍͘͢͢Δ • ίϛοτͷॱ൪ΛೖΕସ͑Δ • ෳ਺ͷίϛοτΛ·ͱΊΔ

    • աڈͷίϛοτϝοηʔδΛมߋ͢Δ • 1౓ίϛοτͨ͠಺༰Λฤू͢Δ
  81. ࣍͸ͪ͜Βʂ

  82. git rebase • ϒϥϯνΛ౷߹͢Δ • ίϛοτཤྺΛݟ΍͘͢͢Δ • ίϛοτͷॱ൪ΛೖΕସ͑Δ • ෳ਺ͷίϛοτΛ·ͱΊΔ

    • աڈͷίϛοτϝοηʔδΛมߋ͢Δ • 1౓ίϛοτͨ͠಺༰Λฤू͢Δ
  83. rebaseͷinteractiveϞʔυ

  84. rebase --interactive or rebase -i

  85. git rebase • ϒϥϯνΛ౷߹͢Δ • ίϛοτཤྺΛݟ΍͘͢͢Δ • ίϛοτͷॱ൪ΛೖΕସ͑Δ • ෳ਺ͷίϛοτΛ·ͱΊΔ

    • աڈͷίϛοτϝοηʔδΛมߋ͢Δ • 1౓ίϛοτͨ͠಺༰Λฤू͢Δ
  86. git rebase -i <commit> $ git log --oneline f65ceba E

    46c578f C dadaaee D abeb89d B 4dd6e33 A $ ίϛοτCͱDͷॱ൪ΛೖΕସ͍͑ͨʂʂ
  87. git rebase -i <commit> $ git log --oneline f65ceba E

    46c578f C dadaaee D abeb89d B 4dd6e33 A $ ίϛοτCͱDͷॱ൪ΛೖΕସ͍͑ͨʂʂ git rebase -i HEAD~3
  88. git rebase -i <commit> pick f65ceba E # Rebase abeb89d..f65ceba

    onto abeb89d (3 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell ..ҎԼলུ.. pick dadaaee D pick 46c578f C ίϛοτCͱDͷॱ൪ΛೖΕସ͍͑ͨʂʂ
  89. git rebase -i <commit> pick f65ceba E # Rebase abeb89d..f65ceba

    onto abeb89d (3 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell ..ҎԼলུ.. pick 46c578f C pick dadaaee D ೖΕସ͑Δ ίϛοτCͱDͷॱ൪ΛೖΕସ͍͑ͨʂʂ
  90. git rebase -i <commit> ίϛοτCͱDͷॱ൪ΛೖΕସ͍͑ͨʂʂ $ git rebase -i HEAD~3

    Auto-merging sample.txt CONFLICT (content): Merge conflict in sample.txt error: could not apply 46c578f... C Resolve all conflicts manually, mark them as resolved with "git add/rm <conflicted_files>", then run "git rebase --continue". You can instead skip this commit: run "git rebase --skip". To abort and get back to the state before "git rebase", run "git rebase --abort". Could not apply 46c578f... C ίϯϑϦΫτΛղফ͢Δʂʂ ↓ git add sample.txt ↓ git rebase --continue
  91. git rebase -i <commit> ίϛοτCͱDͷॱ൪ΛೖΕସ͍͑ͨʂʂ $ git log --oneline 4bfc0bf

    E e1fc7e4 D e319908 C abeb89d B 4dd6e33 A $ ॱ൪ͱίϛοτID͕มΘ͍ͬͯΔ
  92. git rebase • ϒϥϯνΛ౷߹͢Δ • ίϛοτཤྺΛݟ΍͘͢͢Δ • ίϛοτͷॱ൪ΛೖΕସ͑Δ • ෳ਺ͷίϛοτΛ·ͱΊΔ

    • աڈͷίϛοτϝοηʔδΛมߋ͢Δ • 1౓ίϛοτͨ͠಺༰Λฤू͢Δ
  93. git rebase -i <commit> $ git log --oneline 4bfc0bf E

    e1fc7e4 D e319908 C abeb89d B 4dd6e33 A $ ίϛοτBͱCͱDΛ·ͱΊ͍ͨʂʂ git rebase -i HEAD~4
  94. git rebase -i <commit> ίϛοτBͱCͱDΛ·ͱΊ͍ͨʂʂ pick abeb89d B pick 4bfc0bf

    E # Rebase 4dd6e33..4bfc0bf onto 4bfc0bf (4 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message ..ҎԼলུ.. pick e319908 C pick e1fc7e4 D
  95. git rebase -i <commit> ίϛοτBͱCͱDΛ·ͱΊ͍ͨʂʂ pick abeb89d B pick 4bfc0bf

    E # Rebase 4dd6e33..4bfc0bf onto 4bfc0bf (4 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message ..ҎԼলུ.. s e319908 C s e1fc7e4 D ίϛοτΛ1ͭલͷίϛοτʹ౷߹
  96. git rebase -i <commit> ίϛοτBͱCͱDΛ·ͱΊ͍ͨʂʂ # This is a combination

    of 3 commits. # This is the 1st commit message: # This is the commit message #2: # This is the commit message #3: # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # ..ҎԼলུ.. B C D
  97. git rebase -i <commit> ίϛοτBͱCͱDΛ·ͱΊ͍ͨʂʂ # This is a combination

    of 3 commits. # This is the 1st commit message: # This is the commit message #2: # This is the commit message #3: # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # ..ҎԼলུ.. B+C+D
  98. git rebase -i <commit> ίϛοτBͱCͱDΛ·ͱΊ͍ͨʂʂ $ git log --oneline 4bfc0bf

    E 3ba9c8d B+C+D 4dd6e33 A $
  99. git rebase • ϒϥϯνΛ౷߹͢Δ • ίϛοτཤྺΛݟ΍͘͢͢Δ • ίϛοτͷॱ൪ΛೖΕସ͑Δ • ෳ਺ͷίϛοτΛ·ͱΊΔ

    • աڈͷίϛοτϝοηʔδΛมߋ͢Δ • 1౓ίϛοτͨ͠಺༰Λฤू͢Δ
  100. git rebase -i <commit> $ git log --oneline 4bfc0bf E

    3ba9c8d B+C+D 4dd6e33 A $ ίϛοτϝοηʔδΛมߋ͍ͨ͠ʂʂ
  101. git rebase -i <commit> $ git log --oneline 4bfc0bf E

    3ba9c8d B+C+D 4dd6e33 A $ ίϛοτϝοηʔδΛมߋ͍ͨ͠ʂʂ git commit --amend $ git log --oneline 4bfc0bf C 3ba9c8d B+C+D 4dd6e33 A $ 1ͭલͷίϛοτϝοηʔδ͸ git commit --amend ͰมߋͰ͖Δɻ ίϛοτϝοηʔδΛ ʮEʯ͔ΒʮCʯʹมߋɻ
  102. git rebase -i <commit> $ git log --oneline 4bfc0bf E

    3ba9c8d B+C+D 4dd6e33 A $ ίϛοτϝοηʔδΛมߋ͍ͨ͠ʂʂ git commit --amend $ git log --oneline 4bfc0bf C 3ba9c8d B+C+D 4dd6e33 A $ git rebase -i HEAD~2 2ͭҎ্લͷίϛοτϝοηʔδ͸ git rebase -i ͰมߋͰ͖Δɻ
  103. git rebase -i <commit> ίϛοτϝοηʔδΛมߋ͍ͨ͠ʂʂ pick 94dbe7b E # Rebase

    4dd6e33..94dbe7b onto 4dd6e33 (2 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message ..ҎԼলུ.. pick 3ba9c8d B+C+D
  104. git rebase -i <commit> ίϛοτϝοηʔδΛมߋ͍ͨ͠ʂʂ pick 94dbe7b E # Rebase

    4dd6e33..94dbe7b onto 4dd6e33 (2 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message ..ҎԼলུ.. e 3ba9c8d B+C+D
  105. git rebase -i <commit> ίϛοτϝοηʔδΛมߋ͍ͨ͠ʂʂ $ git rebase -i HEAD~2

    Stopped at 3ba9c8d... B+C+D You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue $
  106. git rebase -i <commit> ίϛοτϝοηʔδΛมߋ͍ͨ͠ʂʂ $ git rebase -i HEAD~2

    Stopped at 3ba9c8d... B+C+D You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue $ git commit --amend [detached HEAD 3c3629a] B Date: Sat Oct 19 14:20:11 2019 +0900 1 file changed, 3 insertions(+) $ git rebase --continue Successfully rebased and updated refs/heads/master. --amendͰίϛοτϝοηʔδΛ ʮB+C+Dʯ͔ΒʮBʯʹมߋ
  107. git rebase -i <commit> ίϛοτϝοηʔδΛมߋ͍ͨ͠ʂʂ $ git log --oneline 4bfc0bf

    C 3ba9c8d B 4dd6e33 A $ edit͸ίϛοτϝοηʔδͷมߋ͚ͩͰͳ͘ɺ ϑΝΠϧͷมߋ౳΋Մೳʂʂ
  108. edit͸ࢦఆͨ͠ίϛοτ·Ͱঢ়گ͕໭Γ·͢ɻ ʢࠓճͷ৔߹͸2ͭલͷίϛοτʣ ͳͷͰࢦఆͨ͠ίϛοτͷϑΝΠϧΛमਖ਼͢Δ͜ ͱ΋ՄೳͰ͢ɻ

  109. ࠓճ͸ϑΝΠϧͷมߋ΋Ͱ͖Δͱ͍͏͜ͱͰedit Λ࢖༻͠·͕ͨ͠ɺ ίϛοτϝοηʔδΛม͍͚͑ͨͩͰ͋ΔͳΒ͹ rewordΛࢦఆͨ͠ํ͕؆୯Ͱ͢ʂʂ

  110. ϦΫΤετ ޮՌ p, pick ίϛοτΛ࢖༻͢Δ r, reword ίϛοτϝοηʔδΛฤू e, edit

    ίϛοτ಺༰Λमਖ਼͢Δ s, squash มߋ಺༰Λ௚લͷίϛοτʹ౷߹ f, fixup มߋ಺༰Λ௚લͷίϛοτʹ౷߹ ίϛοτϝοηʔδΛഁغ x, exec γΣϧίϚϯυΛ࣮ߦ d, drop ίϛοτΛ࡟আ
  111. rebaseͷ·ͱΊ

  112. git rebase • ϒϥϯνΛ౷߹͢Δ • ίϛοτཤྺΛݟ΍͘͢͢Δ • ίϛοτͷॱ൪ΛೖΕସ͑Δ • ෳ਺ͷίϛοτΛ·ͱΊΔ

    • աڈͷίϛοτϝοηʔδΛมߋ͢Δ • 1౓ίϛοτͨ͠಺༰Λฤू͢Δ
  113. 2. ࣮຿Ͱͨ·ʹ࢖͏ίϚϯυ • git rebase • git cherry-pick • git

    stash • git revert
  114. cherry-pickͬͯԿʁ

  115. ಛఆͷίϛοτΛऔΓࠐΉ

  116. cherry-pickͷ࢖͍ํ

  117. 1ͭͷίϛοτͷΈΛऔΓࠐΉ • ࢦఆͨ͠ίϛοτΛίϐʔͯ͠HEADͷޙʹ ௥Ճ͢Δ • ίϛοτID͕มΘΔ $ git cherry-pick <commitID>

  118. 1ͭͷίϛοτͷΈΛऔΓࠐΉ $ $ git cherry-pick aaaaaaa aaaaaaa UPQJD NBTUFS bbbbbbb

  119. 1ͭͷίϛοτͷΈΛऔΓࠐΉ $ $ git cherry-pick aaaaaaa aaaaaaa UPQJD NBTUFS Ұ࣌อଘ

    bbbbbbb
  120. 1ͭͷίϛοτͷΈΛऔΓࠐΉ $ $ git cherry-pick aaaaaaa aaaaaaa UPQJD NBTUFS Ұ࣌อଘ

    bbbbbbb
  121. ෳ਺ͷίϛοτΛऔΓࠐΉ • ࢦఆͨ͠ൣғͷίϛοτΛίϐʔͯ͠HEADͷޙʹ௥Ճ͢Δ • ίϛοτID͕มΘΔ • ࢝఺ͷίϛοτID͸औΓࠐΈ͍ͨίϛοτͷ1ͭલʹ͢Δ • ίϛοτͷ௚લΛද͢ʮ^ʯΛར༻͢Ε͹࢝఺ίϛοτ΋ؚΊΔ $

    git cherry-pick <commitID>..<commitID>
  122. ෳ਺ͷίϛοτΛऔΓࠐΉ $ $ git cherry-pick aaaaaaa^..bbbbbbb UPQJD NBTUFS aaaaaaa bbbbbbb

  123. ෳ਺ͷίϛοτΛऔΓࠐΉ $ $ git cherry-pick aaaaaaa^..bbbbbbb UPQJD NBTUFS Ұ࣌อଘ aaaaaaa

    bbbbbbb
  124. ෳ਺ͷίϛοτΛऔΓࠐΉ $ $ git cherry-pick aaaaaaa^..bbbbbbb UPQJD Ұ࣌อଘ ҰͭͣͭదԠ NBTUFS

    aaaaaaa bbbbbbb
  125. ෳ਺ͷίϛοτΛऔΓࠐΉ $ $ git cherry-pick aaaaaaa^..bbbbbbb UPQJD NBTUFS Ұ࣌อଘ ҰͭͣͭదԠ

    aaaaaaa bbbbbbb
  126. cherry-pickͷΦϓγϣϯ

  127. ίϛοτϝοηʔδΛมߋ͍ͨ͠ • ී௨ʹcherry-pickΛ͢Δͱݩͷίϛοτϝο ηʔδͱͳΔ • ʮ-eʯΦϓγϣϯͰϝοηʔδΛมߋͰ͖Δ $ git cherry-pick -e

    <commitID>
  128. ίϛοτΛͤͣʹऔΓࠐΉ • ʮ-nʯΦϓγϣϯͰɺίϛοτͤͣʹऔΓࠐ ΊΔ $ git cherry-pick -n <commitID>

  129. ίϯϑϦΫτ͕ൃੜͨ͠Β • ίϯϑϦΫτղফޙʹ--continueΛ࣮ߦ͢Δ ͜ͱͰcherry-pickͷॲཧ͕࠶։͢Δ $ git cherry-pick --continue

  130. cherry-pickͷ஫ҙࣄ߲

  131. cherry-pickͷ஫ҙࣄ߲ • ίϛοτID͕มΘΔ • ίϯϑϦΫτ͕ൃੜ͢Δ৔߹͕͋Δ • ෳ਺औΓࠐΉͱ͖ͷ࢝఺͸ɺऔΓࠐΈ͍ͨί ϛοτͷ1ͭલΛબͿ • جຊతʹϚʔδϒϥϯν͸औΓࠐΊͳ͍

    ʢʮ-mʯΦϓγϣϯͰՄೳ͕ͩ͋·Γ࢖Θͳ͍ʣ
  132. 2. ࣮຿Ͱͨ·ʹ࢖͏ίϚϯυ • git rebase • git cherry-pick • git

    stash • git revert
  133. git stash ͬͯԿʁ

  134. ίϛοτͤͣʹҰ࣌ୀආ͢Δ

  135. git stashͷ࢖͍ํ

  136. มߋΛҰ࣌తʹୀආ͢Δ • มߋΛҰ࣌తʹୀආ͓ͯ͘͜͠ͱ͕Ͱ͖Δ • addͨ͠ϑΝΠϧ΋add͍ͯ͠ͳ͍ϑΝΠϧ΋ୀආ͞ΕΔ • save ͸লུՄೳ • ৽نϑΝΠϧ͸ୀආ͞Εͳ͍

    $ git stash save
  137. ৽نϑΝΠϧ΋ୀආ͢Δ • --include-untracked • ৽نϑΝΠϧ΋ୀආͤ͞Δ͜ͱ͕Ͱ͖Δ $ git stash save -u

  138. addͨ͠ϑΝΠϧ͸ୀආͨ͘͠ͳ͍ • --keep-index • addͨ͠ϑΝΠϧ͸ୀආ͞Εͳ͍ $ git stash save -k

  139. ୀආ͢Δ࣌ʹίϝϯτΛ͚ͭΔ • stashͨ͠ϦετΛ֬ೝ͢Δ࣌ʹศར $ git stash save “ίϝϯτ”

  140. ୀආͨ͠ҰཡΛ֬ೝ͢Δ • stashͨ͠ϦετΛ֬ೝ͢Δ • git logͷΦϓγϣϯ΋࢖͑Δ • ʮ-pʯΦϓγϣϯͰมߋ಺༰͕֬ೝͰ͖Δ $ git

    stash list
  141. ୀආͨ͠ϑΝΠϧҰཡΛදࣔ • n൪໨ʹୀආͨ͠ϑΝΠϧҰཡΛ֬ೝͰ͖Δ • listͰ൪߸͕֬ೝͰ͖Δ • ʮ-pʯΦϓγϣϯͰมߋ಺༰͕֬ೝͰ͖Δ $ git stash

    show stash@{n}
  142. ୀආͨ͠มߋΛ໭͢ • n൪໨ʹୀආͨ͠มߋ෼Λ໭͢ • stash@{n} Λলུ͢Δͱ࠷৽ͷୀආͨ͠มߋ෼Λ໭͢ • add͍ͯͨ͠มߋ͸add͞Ε͍ͯͳ͍ঢ়ଶͰ໭Δ • มߋ෼Λ໭ͯ͠΋ୀආͨ͠listʹ࢒Δ

    $ git stash apply stash@{n}
  143. addͨ͠ঢ়ଶͰ໭͢ • addͨ͠ঢ়ଶͰͦͷ··໭͍ͨ࣌͠ʹ࢖͑Δ $ git stash apply stash@{n} --index

  144. ୀආͨ͠มߋ෼Λ࡟আ͢Δ • n൪໨ʹୀආͨ͠มߋ෼Λ࡟আ͢Δ • stash@{n} Λলུ͢Δͱ࠷৽ͷୀආͨ͠มߋ෼ Λ࡟আ͢Δ $ git stash

    drop stash@{n}
  145. ୀආͨ͠มߋΛ໭ͯ͠࡟আ͢Δ • applyͱdropΛҰॹʹͰ͖Δ • n൪໨ʹୀආͨ͠มߋ෼Λ໭ͯ͠࡟আ͢Δ • stash@{n} Λলུ͢Δͱ࠷৽ͷୀආͨ͠มߋ෼ ͕ద༻͞ΕΔ $

    git stash pop stash@{n}
  146. ୀආͨ͠มߋ෼Λ͢΂ͯ࡟আ͢Δ • ୀආͨ͠มߋ෼Λ͢΂ͯ࡟আ͢Δ $ git stash clear

  147. ࢦఆͨ͠ϑΝΠϧͷΈ໭͢ • n൪໨ʹୀආͨ͠มߋ෼ͷࢦఆͨ͠ϑΝΠϧΛ ໭͢ • มߋ෼Λ໭ͯ͠΋ୀආͨ͠listʹ࢒Δ $ git checkout stash@{n}

    <ϑΝΠϧ໊>
  148. த్൒୺ͳมߋ͸ίϛοτͤͣʹ stashΛ࢖͏ͱศརʂ

  149. 2. ࣮຿Ͱͨ·ʹ࢖͏ίϚϯυ • git rebase • git cherry-pick • git

    stash • git revert
  150. git revert ͬͯԿʁ

  151. औΓফ͍ͨ͠ίϛοτΛଧͪ ফ͢ίϛοτΛ࡞੒͢Δʂ

  152. git reset ͡Όμϝʁ

  153. • reset͸ίϛοτΛऔΓফͨ͠ͱ͍͏ ཤྺ͕࢒Βͳ͍ • pushࡁΈͷίϛοτΛreset͢Δͷ͸ NGʂ

  154. ϦϞʔτʹpush͞Ε͍ͯΔίϛοτΛ औΓফ͍ͨ࣌͠ͳͲʹ࢖༻Ͱ͖Δ

  155. git revertͷ࢖͍ํ

  156. ࢦఆͨ͠ίϛοτΛऔΓফ͢ • ࢦఆͨ͠ίϛοτIDΛଧͪফ͢ίϛοτΛ৽͘͠࡞੒͢ Δ • ʮ-eʯͰίϛοτϝοηʔδΛมߋͰ͖ΔʢলུՄʣ • ίϛοτϝοηʔδΛมߋ͠ͳ͍৔߹͸ʮ--no-editʯɹ ΦϓγϣϯΛ͚ͭΔ $

    git revert [-e] <commitID>
  157. ίϛοτͤͣʹrevertΛ͢Δ • --no-commit • indexʹ໭͚ͩ͢ͰίϛοτΛߦΘͳ͍ • ෳ਺ͷίϛοτΛ໭ͯ͠ɺ1ͭͷίϛοτͱ͠ ͯpush͢Δ৔߹ʹศར $ git

    revert -n <commitID>
  158. ϚʔδίϛοτΛrevert͢Δ • --mainline • Ϛʔδͨ͠2ͭίϛοτͷ͏ͪɺͲͪΒ͕਌͔Λ਺ࣈͰࢦఆ ͢Δ • git show΍git logͰϚʔδίϛοτ֬ೝ͢Δͱ਺ࣈ͕Θ͔Δ

    • ʮMerge:ʯͱ͍͏ߦͰ֬ೝͰ͖Δʢࠨ͔Β1ɺ2ͱͳΔʣ $ git revert -m <parent-number> <commitID>
  159. revertίϛοτͷ਌Λ Ͳͬͪʹ͢Δͷʁ

  160. masterͷίϛοτΛ਌ʹ͢Δ ͷͰʮ-m 1ʯͱ͢Δʂ

  161. revertͷ·ͱΊ

  162. ͢Ͱʹpushͯ͠͠·ͬͨ commitΛଧͪফ࣌͢ʹ࢖͓͏ʂ

  163. 2. ࣮຿Ͱͨ·ʹ࢖͏ίϚϯυ • git rebase • git cherry-pick • git

    stash • git revert
  164. 3. ͓·͚

  165. rm ͱ git rm ͷҧ͍ͬͯʁ

  166. ྆ํͱ΋ϑΝΠϧΛ࡟আ͢Δ ίϚϯυ

  167. rm • Gitͷཤྺʹ͸࡟আͨ͜͠ͱ͕࢒Βͳ͍ • ϫʔΩϯάπϦʔʹදࣔ͞ΕΔ • Gitͷཤྺʹ࡟আͨ͜͠ͱ͕࢒Δ • indexʹදࣔ͞ΕΔ git

    rm
  168. rmͰϑΝΠϧΛ࡟আͯ͠͠·ͬ ͯ΋େৎ෉ʂ

  169. git add -uͰ indexʹadd͠Α͏ʂʂ

  170. addͷΦϓγϣϯͷछྨ

  171. addͬͯͳΜ͚ͩͬʁ

  172. ϫʔΩϯάπϦʔ͔Β ΠϯσοΫεʹ൓өͤ͞Δ

  173. git add ͷओͳΦϓγϣϯ Φϓγϣϯ ҙຯ -A (-all) มߋ/৽ن/࡟আͨ͠ϑΝΠϧΛadd͢Δ . (υοτ)

    มߋ/৽ن/࡟আͨ͠ϑΝΠϧΛadd͢Δ -u (--update) มߋ/࡟আͨ͠ϑΝΠϧΛadd͢Δ -i (--interactive) ର࿩తʹϑΝΠϧΛadd͢Δ
  174. -A ͱ . ͷҧ͍ͬͯʁ

  175. • ϦϙδτϦ಺ͷͲ͜Ͱ࣮ߦͯ͠΋Ϧϙ δτϦશମΛadd͢Δ • ΧϨϯτσΟϨΫτϦҎԼͷϑΝΠϧ ͷΈΛadd͢Δ git add . git

    add -A
  176. ΤΠϦΞεͷઃఆ

  177. configʹΤΠϦΞεΛઃఆ͢Δ • git status ͱଧͨͳͯ͘΋ git st Ͱ࣮ߦͰ͖Δ • ϦϙδτϦ୯ҐͰઃఆ͢ΔͳΒ

    --local ͱ͢Δ • git config --global -e ͱ͢ΔͱΤσΟλ͕։͘ͷ Ͱɺ௚઀هड़͢Δ͜ͱ΋Ͱ͖Δ $ git config --global alias.st status
  178. ΤΠϦΞεͷઃఆྫ [alias] st = status sts = status --short --branch

    cm = commit co = checkout br = branch df = diff lg = log --oneline --graph -10 ss = stash
  179. ஫ҙ • ͢Ͱʹଘࡏ͢ΔίϚϯυΛ্ॻ͖͢Δ͜ͱ͸ Ͱ͖ͳ͍ [alias] status = status --short --branch

  180. switch ͱ restore

  181. ͳΜͰ௥Ճ͞Εͨͷʁ

  182. checkoutͷػೳΛ෼͚ΔͨΊ

  183. • ϒϥϯνͷ੾Γସ͑ • ϑΝΠϧͷมߋ restore switch

  184. ϒϥϯνͷ੾Γସ͑ • git checkout <branch໊> ͱಉ͡ • Φϓγϣϯʹʮ-cʯΛ෇͚ΔͱϒϥϯνΛ৽ ن࡞੒ͯ͠੾Γସ͑Δɹɹɹɹɹɹɹɹɹɹ git

    checkout -b <branch໊>ͱಉ͡ $ git switch <branch໊>
  185. ϑΝΠϧͷมߋ෼Λ࡟আ͢Δ • git checkout <file໊> ͱಉ͡ $ git restore <file໊>

  186. checkout΋ࠓ·Ͱ௨Γ࢖͑Δ

  187. Ҏ্Ͱ͢ʂʂ

  188. ·ͱΊ

  189. Git͸ͱͯ΋ਂ͍ʂʂʂʂ

  190. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ @yyykms123 / @akito_tameto