はじめに
現在作業途中だけど、他のブランチで別の作業を行わなければならないから、現在の作業はコミットせずにファイルを一時退避させたいというときに、git stash
を使います。
git commit
していないファイルが対象なので、git add
はしていてもしていなくても退避させることができます。
コマンド
git stash
・ワークツリー及びステージにあるファイルを一時退避させます。
・前回のコミット後に新規作成されたファイルは退避に含まれません。
・saveはあってもなくても良いです。
・複数回git stash
を行うと、stash{0}
stash{1}
stash{2}
といった感じで何回目のstashかが分かるようになります。
ターミナル
git stash
git stash save
git stash -u
・git stash
と異なり、前回のコミット後に新規作成されたファイルも退避に含まれます。
ターミナル
git stash -u
git stash list
・一時退避させた作業の一覧を確認することができます。
ターミナル
git stash list
git stash apply
・一時退避させた作業の復元を行います。
ターミナル
git stash apply //最新の作業(1番直近で退避した作業)を復元
git stash apply stash{1} //stash{1}という特定の作業を復元
git stash apply --index //addされたものはaddされたままの状態で復元
git stash drop
・一時退避した作業を削除します。
ターミナル
git stash drop //最新の作業(1番直近で退避した作業)を削除
git stash drop stash{1} //stash{1}という特定の作業を削除
git stash clear //すべての作業を削除