Git Stash
You are halfway through a change when an urgent bug report arrives. Your work isn't ready to commit, but you need a clean working tree to fix the bug. git stash puts your uncommitted changes on a shelf, gives you a clean tree, and lets you take them back later.
Stash and restore
mkdir blog && cd blog
git init -q
echo "Title: Hello" > post.md && git add post.md && git commit -q -m "Add post"
echo "Draft paragraph about Git" >> post.md
git status -s
git stash
git status -s
echo "(clean: the draft is stashed)"
cat post.md
git stash pop
cat post.md M post.md
Saved working directory and index state WIP on main: c865bd5 Add post
(clean: the draft is stashed)
Title: Hello
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: post.md
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (c6aeff234500a420dad845b97747c28deb25826f)
Title: Hello
Draft paragraph about Git
git stash saved the change and reset the file to the last commit. git stash pop re-applied it and removed it from the stash list.
Several stashes
Stashes form a stack. stash@{0} is the newest. Give each one a message with git stash push -m so you know what is inside:
mkdir blog && cd blog
git init -q
echo "intro" > post.md && echo "body {}" > style.css
git add . && git commit -q -m "Start"
echo "new intro" > post.md
git stash push -m "rewrite intro"
echo "body { margin: 0; }" > style.css
git stash push -m "reset margins"
git stash list
echo "--- contents of stash@{1}:"
git stash show -p stash@{1}Saved working directory and index state On main: rewrite intro
Saved working directory and index state On main: reset margins
stash@{0}: On main: reset margins
stash@{1}: On main: rewrite intro
--- contents of stash@{1}:
diff --git a/post.md b/post.md
index ab5aaf3..32ceb5b 100644
--- a/post.md
+++ b/post.md
@@ -1 +1 @@
-intro
+new intro
apply vs pop vs drop
| Command | Effect |
|---|---|
git stash pop |
apply the newest stash and remove it from the list |
git stash apply stash@{1} |
apply a stash but keep it in the list |
git stash drop stash@{1} |
delete one stash |
git stash clear |
delete all stashes |
git stash branch new-branch |
create a branch from the stash's base commit and apply it there |
mkdir blog && cd blog
git init -q
echo "intro" > post.md && git add . && git commit -q -m "Start"
echo "draft A" > post.md && git stash push -q -m "draft A"
echo "draft B" > post.md && git stash push -q -m "draft B"
git stash apply -q stash@{1}
cat post.md
git stash list
git checkout -q -- post.md
git stash drop stash@{1}
git stash listdraft A
stash@{0}: On main: draft B
stash@{1}: On main: draft A
Dropped stash@{1} (e210cf2570bd0255d05a57be84e1352758302a74)
stash@{0}: On main: draft B
Untracked files
By default only changes to tracked files are stashed. A brand-new file stays in the folder. Add -u (--include-untracked) to stash it too:
mkdir blog && cd blog
git init -q
echo "intro" > post.md && git add . && git commit -q -m "Start"
echo "todo" > notes.txt
echo "edit" >> post.md
git stash -q
git status -s
echo "--- with -u:"
git stash pop -q
git stash -q -u
git status -s
echo "(nothing left)"
git stash pop -q
git status -s?? notes.txt
--- with -u:
(nothing left)
M post.md
?? notes.txt
When popping conflicts
If the files changed since you stashed, pop can produce a merge conflict. In that case Git applies what it can, marks the conflict, and keeps the stash in the list so nothing is lost. Resolve the conflict, then git stash drop.
Tip: Stashes are easy to forget. For anything you will not resume within the hour, a work-in-progress commit on a branch is safer and visible to
git log.
Test yourself
What is the difference between git stash pop and git stash apply?
- There is none
- pop also removes the stash from the list; apply keeps it
- apply also removes the stash; pop keeps it
- pop only works on untracked files
You run git stash with a new untracked file in the folder. What happens to that file?
- It is stashed
- It stays in the working folder
- It is deleted
- It is committed
Which stash is stash@{0}?
- The oldest
- The most recent
- The one with the shortest message
- The one on the main branch