目的: Commitしたくない作業途中の変更を、安全に退避して戻す 目安: 15分
feature/login を作業中
↓
「今すぐ本番のバグを見てほしい」と言われた
↓
でも中途半端なのでCommitしたくない
↓
git stash
Branchを移動しようとすると、Gitに止められることがあります。
error: Your local changes to the following files would be overwritten by checkout
このときに使うのが stash です。
git switch main
git pull
git switch -c training/<your-name>-stashmembers/<your-name>.md の末尾に追加して保存します。
## Stash Training
作業途中の状態です。git status
git diffgit stashSaved working directory and index state WIP on training/taro-stash: ...
git statusnothing to commit, working tree clean
変更が消えたように見えます。 でも失われてはいません。
エディタでファイルを開いて、追加した行がなくなっていることも確認してください。
git switch main
git status問題なく移動できます。これがstashの目的です。
git switch training/<your-name>-stashgit stash liststash@{0}: WIP on training/taro-stash: 8f2c1ab Add Taro training profile
中身を見ることもできます。
git stash show -p stash@{0}git stash popgit status
git diff追加した行が戻っていれば成功です。
git stash listpop したので、一覧からは消えています。
| コマンド | 意味 |
|---|---|
git stash |
変更を退避する |
git stash list |
退避の一覧を見る |
git stash show -p |
退避の中身を見る |
git stash pop |
取り出して一覧から削除 |
git stash apply |
取り出すが一覧に残す |
git stash drop |
取り出さずに捨てる |
- stashは溜めないでください。 何個も積むと、どれが何か分からなくなります
- 新規作成してまだ
addしていないファイルは、標準では退避されません(git stash -uが必要) - 長期間の退避には向きません。長引くなら、作業Branchを作ってCommitするほうが安全です
git restore members/<your-name>.md
git switch main
git branch -d training/<your-name>-stash自分のIssueで、実習5のチェックを付けてください。