stashとcherry-pick
💾 stashと🍒 cherry-pick
Section titled “💾 stashと🍒 cherry-pick”作業中の変更を一時的に保存するstashと、特定のコミットを選択的に適用するcherry-pickについて詳しく解説します。
💾 stash(スタッシュ)
Section titled “💾 stash(スタッシュ)”📋 stashとは
Section titled “📋 stashとは”📋 定義:
stashは、作業中の変更を一時的に保存し、作業ディレクトリをクリーンな状態にするコマンドです。
基本的な使い方:
# 変更を一時的に保存git stash
# メッセージを付けて保存git stash save "作業中の変更を保存"
# 変更を復元git stash pop
# 変更を復元(stashを削除しない)git stash applystashの一覧表示
Section titled “stashの一覧表示”# stashの一覧を表示git stash list
# 出力例:# stash@{0}: WIP on main: abc1234 最新のコミット# stash@{1}: WIP on feature: def5678 前のコミット
# 詳細情報を表示git stash show
# 特定のstashの詳細を表示git stash show stash@{0}
# 差分を表示git stash show -p stash@{0}stashの適用と削除
Section titled “stashの適用と削除”# 最新のstashを適用して削除git stash pop
# 特定のstashを適用して削除git stash pop stash@{1}
# 最新のstashを適用(削除しない)git stash apply
# 特定のstashを適用(削除しない)git stash apply stash@{1}
# stashを削除git stash drop stash@{0}
# すべてのstashを削除git stash clearstashの実践例
Section titled “stashの実践例”基本的な使用:
# 1. 作業中の変更を保存git stash# Saved working directory and index state WIP on main: abc1234
# 2. ブランチを切り替えgit checkout feature/other-feature
# 3. 作業を完了
# 4. 元のブランチに戻るgit checkout main
# 5. 保存した変更を復元git stash pop複数のstashの管理:
# 1. 最初の変更を保存git stash save "ユーザー認証の実装"
# 2. 別の変更を保存git stash save "商品一覧の実装"
# 3. stashの一覧を確認git stash list# stash@{0}: On main: 商品一覧の実装# stash@{1}: On main: ユーザー認証の実装
# 4. 特定のstashを適用git stash apply stash@{1}未追跡ファイルも含めて保存:
# 未追跡ファイルも含めて保存git stash -u# またはgit stash --include-untracked
# すべての変更を保存(未追跡ファイル、無視されたファイルも含む)git stash -a# またはgit stash --allcherry-pick(チェリーピック)
Section titled “cherry-pick(チェリーピック)”cherry-pickとは
Section titled “cherry-pickとは”定義:
cherry-pickは、別のブランチの特定のコミットを現在のブランチに適用するコマンドです。
基本的な使い方:
# 特定のコミットを適用git cherry-pick <コミットハッシュ>
# 複数のコミットを適用git cherry-pick <コミット1> <コミット2>
# 範囲でコミットを適用git cherry-pick <開始コミット>..<終了コミット>cherry-pickの実践例
Section titled “cherry-pickの実践例”基本的な使用:
# 1. コミットハッシュを確認git log --oneline# abc1234 ユーザー認証機能を実装# def5678 商品一覧機能を実装# ghi9012 バグ修正
# 2. 特定のコミットを適用git checkout maingit cherry-pick abc1234
# 3. 複数のコミットを適用git cherry-pick abc1234 def5678
# 4. 範囲でコミットを適用git cherry-pick abc1234..ghi9012コンフリクトの解決:
# cherry-pick時にコンフリクトが発生git cherry-pick abc1234# error: could not apply abc1234# hint: after resolving the conflicts, mark the corrected paths
# コンフリクトファイルを確認git status
# コンフリクトを解決# ファイルを編集してコンフリクトマーカーを削除
# 解決後、ステージングと続行git add .git cherry-pick --continue
# cherry-pickを中止する場合git cherry-pick --abortコミットメッセージを変更:
# コミットメッセージを編集して適用git cherry-pick -e abc1234# またはgit cherry-pick --edit abc1234
# コミットメッセージを使用せずに適用git cherry-pick -n abc1234# またはgit cherry-pick --no-commit abc1234実践的な使用例
Section titled “実践的な使用例”1. 緊急修正を別ブランチに適用
Section titled “1. 緊急修正を別ブランチに適用”# 1. mainブランチで緊急修正git checkout maingit commit -m "緊急バグ修正"# コミットハッシュ: abc1234
# 2. developブランチにも同じ修正を適用git checkout developgit cherry-pick abc12342. 特定の機能だけを別ブランチに適用
Section titled “2. 特定の機能だけを別ブランチに適用”# 1. featureブランチの特定のコミットを確認git checkout feature/user-authgit log --oneline# abc1234 ユーザー認証機能を実装# def5678 ログイン機能を実装# ghi9012 パスワードリセット機能を実装
# 2. ログイン機能だけをmainブランチに適用git checkout maingit cherry-pick def56783. 作業中の変更を一時保存してブランチを切り替え
Section titled “3. 作業中の変更を一時保存してブランチを切り替え”# 1. 作業中の変更を保存git stash save "作業中の変更"
# 2. 別のブランチに切り替えて作業git checkout feature/urgent-fix# 緊急修正を実施git add .git commit -m "緊急修正"git push origin feature/urgent-fix
# 3. 元のブランチに戻るgit checkout feature/original-work
# 4. 保存した変更を復元git stash popstashとcherry-pickのポイント:
- stash: 作業中の変更を一時保存、作業ディレクトリをクリーンな状態にする
- stashの管理: 一覧表示、適用、削除、複数のstashの管理
- cherry-pick: 特定のコミットを選択的に適用
- cherry-pickの実践: 基本的な使用、コンフリクトの解決、コミットメッセージの変更
- 実践的な使用例: 緊急修正の適用、特定機能の適用、作業中の変更の一時保存
適切にstashとcherry-pickを使用することで、柔軟に開発を進めることができます。