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)
index.html の変更が戻り、一時置き場の stash@{0} は消えた(Dropped)

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 トップページを作る
apply は戻したあとも、一時置き場に残る

いらなくなったら、あとで 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: 紅茶を足す
stash@{1}(営業時間)だけを戻した。紅茶の方は残っている

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 changes
衝突した時は、一時置き場から消えずに残る

Updated upstream の側が今のファイル、Stashed changes の側がしまっていた変更です。残したい形に書き直して、<<<<<<<・=======・>>>>>>> の行を消します。そのあと、次の順に打ちました。

衝突を直したあと

git add menu.txt
git restore --staged menu.txt
git stash drop

git 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.
手元の index.html の変更が邪魔をしている

新しく作ったファイルが、しまわれていなかった

一度もコミットしていない新しいファイル(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 なしでは style.css が手元に残り、-u を付けると一緒にしまわれた

-u でしまった物も、git stash show だけでは新しいファイルが一覧に出ず、--include-untracked を付けると出ました。戻す時は、いつもと同じく git stash pop です。また、git add 済みだった変更は、pop すると「add していない変更」として戻りました(新しく作って add したファイルは、add された状態のまま戻りました)。add した状態まで戻したい時は git stash pop --index を使います。

  1. git stash listしまった物と番号を見る
  2. git stash show -p stash@{番号}戻す前に中身を確かめる
  3. git stash pop(残すなら apply)番号を付けなければ stash@{0} が戻る
  4. 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で、ほかによくあるつまずき