git stash を打ったら、書きかけの変更が画面から消えた。消えたのではなく、Git の「一時置き場」にしまわれています。いちばん最後にしまった1つを戻すなら、次の1行です。
まずこれを打つ
git stash pop戻すと一時置き場からは消えます。消さずに戻したい時は git stash apply。いくつもしまった時は、先に git stash list で番号を見ます(下の表)。
何が起きているか
git stash は、まだコミットしていない変更を一時置き場にしまい、作業中のファイルを最後のコミットの状態に戻す命令です。急いで別の枝(ブランチ)に移りたい時などに使います。しまった物は git stash list で見られます。
下は、index.html に営業時間の行を足した状態で git stash を打ち、git stash pop で戻した画面です。
hanako@MacBook-Air cafe % git stash
Saved working directory and index state WIP on main: 49c9563 トップページを作る
hanako@MacBook-Air cafe % git stash list
stash@{0}: WIP on main: 49c9563 トップページを作る
hanako@MacBook-Air cafe % git stash pop
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (00ed11c6b3d0df88d26a67ea5ceaa8a5d1a1f618)stash@{0} が、いちばん新しくしまった物の名前です。WIP on main は「main の枝で作業中だった物」という意味で、その後ろは、しまった時の最後のコミットです。最後の Dropped の行は、一時置き場から消したという知らせです。
場面の見分け方
| いまの様子 | 場面 | 打つもの |
|---|---|---|
git stash list に1つだけ | 直前の1つを戻す | git stash pop |
| 戻したあとも、しまった物を残しておきたい | 消さずに戻す | git stash apply |
git stash list に2つ以上 | 戻す物を選ぶ | git stash show -p stash@{1} で中身を見て git stash pop stash@{1} |
pop したら CONFLICT と出た | 戻す変更と、今のファイルがぶつかった | 手で直して git add → git restore --staged、最後に git stash drop |
pop したら Your local changes … would be overwritten | 手元に、まだコミットしていない変更がある | 先にコミットしてから pop |
| 新しく作ったファイルが、しまわれていなかった | 追跡していないファイルは、ふつうはしまわれない | 次から git stash -u |
git stash drop で消してしまった | 一時置き場から消えた | Dropped の行の ID で git stash apply ID |
pop と apply の違い
どちらも変更を戻しますが、pop は戻したあとに一時置き場から消し、apply は残します。同じ状態から apply で戻すと、git stash list に残ったままでした。
hanako@MacBook-Air cafe % git stash apply
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
hanako@MacBook-Air cafe % git stash list
stash@{0}: WIP on main: 49c9563 トップページを作るいらなくなったら、あとで git stash drop で消します。迷ったら apply で戻し、うまくいったのを確かめてから drop すると、戻し損ねても一時置き場に残っています。
いくつもしまった時は、番号を選ぶ
git stash を何回も打つと、新しい物から順に stash@{0}、stash@{1} と番号が付きます。しまう時に git stash push -m "メモ" とメモを付けておくと、一覧で見分けやすくなります。
hanako@MacBook-Air cafe % git stash list
stash@{0}: On main: 紅茶を足す
stash@{1}: On main: 営業時間
hanako@MacBook-Air cafe % git stash show stash@{1}
index.html | 1 +
1 file changed, 1 insertion(+)
hanako@MacBook-Air cafe % git stash pop stash@{1}
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{1} (d1e9af1be35a9f47e8156f7ce5dbfdb10273af15)
hanako@MacBook-Air cafe % git stash list
stash@{0}: On main: 紅茶を足すgit stash show は変わったファイルの一覧、-p を付けると変えた行まで出ます。番号は、しまったり戻したりするたびに付け直されるので、続けて戻す時は、そのたびに git stash list で見直します。
pop で衝突した時は、stash は消えない
しまった後で同じ行を変えてコミットしていると、戻す時にぶつかります。下は、menu.txt に紅茶の行を足した変更をしまい、その後で同じ所にケーキの行を足してコミットしてから、pop した画面です(git status と同じ案内の行は省いています)。
hanako@MacBook-Air cafe % git stash pop
Auto-merging menu.txt
CONFLICT (content): Merge conflict in menu.txt
(省いています)
The stash entry is kept in case you need it again.
hanako@MacBook-Air cafe % git stash list
stash@{0}: On main: 紅茶を足す
hanako@MacBook-Air cafe % cat menu.txt
coffee 400
<<<<<<< Updated upstream
cake 500
=======
tea 450
>>>>>>> Stashed changesUpdated upstream の側が今のファイル、Stashed changes の側がしまっていた変更です。残したい形に書き直して、<<<<<<<・=======・>>>>>>> の行を消します。そのあと、次の順に打ちました。
衝突を直したあと
git add menu.txt
git restore --staged menu.txt
git stash dropgit add で「直した」と Git に伝えると、git status の both modified(両方で変えた)の表示が消えます。restore --staged で、ふつうの「まだ add していない変更」に戻します。stash は自動では消えないので、最後に drop で消します。
直すのをやめて pop する前に戻したい時は、git restore --staged --worktree menu.txt で、そのファイルが最後のコミットの状態に戻りました。stash は残っているので、あとでやり直せます。
手元に変更があって、pop できない時
まだコミットしていない変更と、しまった変更が同じファイルにあると、pop は始まる前に止まりました。この時も stash は残ります。手元の変更を先にコミットしてから、もう一度 git stash pop を打ちます。
hanako@MacBook-Air cafe % git stash pop error: Your local changes to the following files would be overwritten by merge: index.html Please commit your changes or stash them before you merge. Aborting (省いています) The stash entry is kept in case you need it again.
新しく作ったファイルが、しまわれていなかった
一度もコミットしていない新しいファイル(git status で ?? と出る物)は、git stash だけではしまわれず、手元にそのまま残りました。消えたわけではありません。新しいファイルも一緒にしまいたい時は、-u(--include-untracked)を付けます。
hanako@MacBook-Air cafe % git status --short M index.html ?? style.css hanako@MacBook-Air cafe % git stash Saved working directory and index state WIP on main: c5aeeda トップページを作る hanako@MacBook-Air cafe % git status --short ?? style.css hanako@MacBook-Air cafe % git stash pop -q hanako@MacBook-Air cafe % git stash -u Saved working directory and index state WIP on main: c5aeeda トップページを作る hanako@MacBook-Air cafe % git status --short hanako@MacBook-Air cafe % git stash show --include-untracked index.html | 1 + style.css | 1 + 2 files changed, 2 insertions(+)
-u でしまった物も、git stash show だけでは新しいファイルが一覧に出ず、--include-untracked を付けると出ました。戻す時は、いつもと同じく git stash pop です。また、git add 済みだった変更は、pop すると「add していない変更」として戻りました(新しく作って add したファイルは、add された状態のまま戻りました)。add した状態まで戻したい時は git stash pop --index を使います。
git stash listしまった物と番号を見るgit stash show -p stash@{番号}戻す前に中身を確かめるgit stash pop(残すならapply)番号を付けなければstash@{0}が戻るCONFLICTと出たら、直してgit stash drop衝突した時、stash は自動では消えない
stash が役立つ「作りかけを置いて、別の枝に移る」流れは、図解の Git ブランチ入門 で、コミットや git add の意味は Git 入門 で説明しています。add した変更を戻したい時は git add を取り消したい も参考になります。
この記事の画面は、macOS 26.6.2 / zsh 5.9 / git 2.51.0 で実際に出したものです。
Gitで、ほかによくあるつまずき
git branch -d で error: the branch 'x' is not fully merged と出て消せないbranch -d で not fully mergedgit switch で fatal: invalid reference: <name> と出て枝に移れないgit switch で invalid reference消してしまったファイルを、git で元に戻したいgit で削除したファイルを戻すremote: Repository not found. / fatal: repository '...' not foundgit clone で Repository not foundgit merge で Already up to date. と出て、取り込みたい変更が入らないmerge で Already up to date.[email protected]: Permission denied (publickey).Permission denied (publickey)