Using git stash to set aside uncommitted changes
Sometimes you want to switch to another branch in the middle of your work, but your uncommitted changes conflict with the contents of the target branch, and git switch stops. With git stash, you can set aside changes you don't want to commit yet and return your working files to the state of the last commit. You can bring the set-aside changes back later with git stash pop.
I tested on Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>), and I fixed the commit dates with environment variables so the output would be the same every time. I used only repositories I created locally and did not test anything involving a remote. I did not test on Windows or macOS.
- 1. When local changes block a branch switch
- 2. Restoring set-aside changes with git stash pop
- 3. Untracked new files are not set aside without -u
- 4. Stashing several changes with messages
- 5. apply restores changes but keeps them in the list
- 6. Setting aside only some files
- 7. Staged changes are not restored by pop alone
- 8. When pop hits a conflict, the entry stays in the list
- 9. Recovering a change that was dropped by mistake
When local changes block a branch switch
I added one line to index.html on the main branch and did not commit it. When I tried to switch to the hotfix branch, which has a different version of index.html committed, Git stopped me.
$ git status --short
M index.html
$ git switch hotfix
error: Your local changes to the following files would be overwritten by checkout:
index.html
Please commit your changes or stash them before you switch branches.
Aborting
As the message says, you need to either commit the changes or stash them. If you don't want to commit the unfinished changes, use git stash to set them aside.
$ git stash
Saved working directory and index state WIP on main: ba50b1e 最初のコミット
$ git status
On branch main
nothing to commit, working tree clean
$ git stash list
stash@{0}: WIP on main: ba50b1e 最初のコミット
$ cat index.html
<h1>ねこノート</h1>
The added line was removed from index.html, and the file matched the previous commit (ba50b1e). The set-aside changes remain in git stash list as stash@{0}. In this state, I was able to switch branches.
$ git switch hotfix
Switched to branch 'hotfix'
$ git switch main
Switched to branch 'main'
Restoring set-aside changes with git stash pop
After switching back to main, I ran git stash pop. The set-aside changes returned to the working files and were removed from the list.
$ 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} (46113263a6792c883cbc2f408787daa37d5c53eb)
$ cat index.html
<h1>ねこノート</h1>
<p>新しい記事の一覧</p>
$ git stash list
According to the official git stash reference, running git stash with no arguments is the same as git stash push. Newer set-aside changes are placed on top, so the latest one is stash@{0} and the one before it is stash@{1}.

Untracked new files are not set aside without -u
git stash alone does not set aside files that Git is not tracking yet, meaning files you have never run git add on. I tested this by making changes to index.html and also creating a new file, memo.txt.
$ git status --short
M index.html
?? memo.txt
$ git stash
Saved working directory and index state WIP on main: ba50b1e 最初のコミット
$ git status --short
?? memo.txt
The changes to index.html were set aside, but memo.txt stayed where it was. I ran pop to restore everything, then set the changes aside again with -u (--include-untracked), and this time memo.txt was removed from the folder too.
$ git stash -u
Saved working directory and index state WIP on main: ba50b1e 最初のコミット
$ git status --short
$ ls
README.md
index.html
$ git stash show --include-untracked
index.html | 1 +
memo.txt | 1 +
2 files changed, 2 insertions(+)
Adding --include-untracked to git stash show lets you confirm that memo.txt is included in the stash. After I ran git stash pop, both the changes to index.html and memo.txt were restored.
Stashing several changes with messages
If you don't give a name, set-aside changes appear in the list as WIP on main: .... When you have several, they are hard to tell apart, so it helps to add a description with git stash push -m. I set aside the changes to index.html and then the changes to README.md separately.
$ git stash push -m '記事の一覧を追加'
Saved working directory and index state On main: 記事の一覧を追加
$ echo 'トップページは index.html です。' >> README.md
$ git stash push -m 'README に説明を追加'
Saved working directory and index state On main: README に説明を追加
$ git stash list
stash@{0}: On main: README に説明を追加
stash@{1}: On main: 記事の一覧を追加
The later entry, which I named "Add description to README", became stash@{0}, and the earlier one moved down to stash@{1}. You can check the contents with git stash show. Adding -p shows the actual diff.
$ git stash show stash@{1}
index.html | 1 +
1 file changed, 1 insertion(+)
$ git stash show -p stash@{1}
diff --git a/index.html b/index.html
index e54fb8d..2c2575e 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
<h1>ねこノート</h1>
+<p>新しい記事の一覧</p>
apply restores changes but keeps them in the list
git stash apply also restores set-aside changes. The difference from pop is that it does not remove the entry from the list after restoring. You can specify a number to restore an entry other than the top one.
$ git stash apply 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")
$ git stash list
stash@{0}: On main: README に説明を追加
stash@{1}: On main: 記事の一覧を追加
The changes to index.html were restored, and stash@{1} is still in the list. If you no longer need it, remove it from the list with git stash drop.
$ git stash drop stash@{1}
Dropped stash@{1} (db0f8265a2ad15daf85ac1064e7e3f526e99902f)
$ git stash list
stash@{0}: On main: README に説明を追加
Each of the following sections starts from a clean state: the previous section's changes have been cleaned up, there are no changes in the working files, and git stash list is empty.
Setting aside only some files
If you add -- and a file name after git stash push, only the changes to that file are set aside. With changes to both index.html and README.md, I set aside only README.md.
$ git status --short
M README.md
M index.html
$ git stash push -m 'README だけ' -- README.md
Saved working directory and index state On main: README だけ
$ git status --short
M index.html
The changes to index.html remain in the working files.
Staged changes are not restored by pop alone
I added one line to index.html, staged it with git add, and set it aside. After restoring it with git stash pop, the content came back, but the change was no longer staged.
$ echo '<p>新しい記事の一覧</p>' >> index.html
$ git add index.html
$ git status --short
M index.html
$ git stash
Saved working directory and index state WIP on main: ba50b1e 最初のコミット
$ 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} (d3f8d19049703dd3fa70f6314c490cd115f5376c)
$ git status --short
M index.html
In git status --short, the M moved from the left column (staged changes) to the right column (unstaged changes). To restore the staged state as well, use --index.
$ git add index.html
$ git stash
Saved working directory and index state WIP on main: ba50b1e 最初のコミット
$ git stash pop --index
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Dropped refs/stash@{0} (ad6f944cd5e5a9a674581e590a6b60673fd30a1c)
$ git status --short
M index.html
When pop hits a conflict, the entry stays in the list
If, while the changes were set aside, you changed the same part of the same file and committed it, pop will conflict. I added a line meaning "New article list" to line 2 of index.html and set it aside. Then I added a line meaning "Announcement" to the same line 2 and committed it, and only after that ran pop.
$ git stash pop
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
On branch main
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
The stash entry is kept in case you need it again.
$ git stash list
stash@{0}: WIP on main: ba50b1e 最初のコミット
$ cat index.html
<h1>ねこノート</h1>
<<<<<<< Updated upstream
<p>お知らせ</p>
=======
<p>新しい記事の一覧</p>
>>>>>>> Stashed changes
The output says The stash entry is kept in case you need it again., so the set-aside changes remain in the list. The Updated upstream side is the committed content, and the Stashed changes side is the content that was set aside. The official reference also says that a conflicting stash is not removed from the list, so you should resolve the conflict by hand and then run git stash drop.
I rewrote index.html to keep both lines, ran git add, and then ran drop.
$ printf '<h1>ねこノート</h1>\n<p>お知らせ</p>\n<p>新しい記事の一覧</p>\n' > index.html
$ git add index.html
$ git stash drop
Dropped refs/stash@{0} (75e93a433e3c9d37c0776d4252c3ddd9fd558036)
$ git stash list
$ git status --short
M index.html
Because I ran git add, index.html is in the staged state. Dropping the entry does not change the working files.
Recovering a change that was dropped by mistake
When you remove an entry from the list with pop or drop, the output shows the hash of the commit that recorded the stash inside the parentheses in Dropped refs/stash@{0} (...). According to the official reference, apply accepts a commit created by stash even if it is no longer in the list. I added one line to index.html, set it aside with git stash, and dropped it right away. Passing the displayed hash to git stash apply restored the deleted changes.
$ echo '<p>新しい記事の一覧</p>' >> index.html
$ git stash
Saved working directory and index state WIP on main: c092d3b お知らせを追加
$ git stash drop
Dropped refs/stash@{0} (a08e548fed8dd6ee3a4a458629767d3e6c3e1f8e)
$ git stash list
$ git stash apply a08e548
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")
$ git status --short
M index.html
According to the official reference, changes removed from the list with drop or with git stash clear, which removes all entries, cannot be recovered through the usual safety mechanisms. The official reference's examples also show how to use git fsck --unreachable to search for changes that were removed. Before you delete anything, check its contents with git stash show -p.





