Skip to content
Laviyx Learn

Try

Git and GitHub course menu

git stash: Save Unfinished Work Without Committing

Put unfinished changes aside with git stash, bring them back with stash pop and apply, list and name stashes, include untracked files, and move work to a new branch.

Beginner7 min readUpdated
On this page

You are halfway through a feature when someone asks you to fix an urgent bug on another branch. Your work is not ready to commit. git stash puts it aside and gives you a clean working tree.

Stash and bring back#

Terminal
echo "Half-finished banner" >> index.html
git status --short
git stash
git status --short
Output
 M index.html
Saved working directory and index state WIP on main: 4e04c7b Add home page

The edit is gone from the file, and the working tree is clean. Do the urgent work, then bring it back with git stash pop:

Terminal
git stash pop
Output
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} (817bc612563edef73066ec2a40445254eb51df5d)

Name your stashes#

Give a stash a message so you can recognise it later:

Terminal
git stash push -q -m "banner work"
echo "Trying a new font" >> index.html
git stash push -q -m "font experiment"
git stash list
Output
stash@{0}: On main: font experiment
stash@{1}: On main: banner work

Stashes form a stack: stash@{0} is the newest. Restore a particular one by number:

Terminal
git stash apply stash@{1}
git stash list
Output
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")
stash@{0}: On main: font experiment
stash@{1}: On main: banner work
CommandRestores the changes?Removes the stash?
git stash popYesYes, if there is no conflict
git stash applyYesNo
git stash dropNoYes
git stash clearNoRemoves all stashes

See what is inside#

Terminal
git stash show -p stash@{0}
Output
diff --git a/index.html b/index.html
index 6a2c9c8..a17f5c6 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
 Home page
+Trying a new font

Without -p you get a summary of the files.

Untracked files#

By default, git stash ignores new files. Add -u to include them:

Terminal
git stash clear
echo "New page" > new.html
git stash
git status --short
Output
No local changes to save
?? new.html
Terminal
git stash -u
git status --short
git stash list
Output
Saved working directory and index state WIP on main: 4e04c7b Add home page
stash@{0}: WIP on main: 4e04c7b Add home page

No local changes to save appeared the first time because the only change was an untracked file. The second time, -u saved it.

Move stashed work to a branch#

If the stashed work turns out to be a real feature, this creates a branch from the commit where you stashed it, and applies the stash there:

Terminal
git stash -q -u
git stash branch new-page
Output
Switched to a new branch 'new-page'
Already up to date.
On branch new-page
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	new.html

nothing added to commit but untracked files present (use "git add" to track)
Dropped refs/stash@{0} (8af8b9d2f47f4c93f22314a05e4230e2dc33ac8b)

Conflicts#

If the file changed in the meantime, git stash pop can stop with a conflict, just like a merge. Fix it in the same way. The stash is kept in that case, so run git stash drop once you are happy.

When not to stash#

A stash is for a few minutes or hours. Stashes are easy to forget, and they are not pushed to GitHub. For anything longer, commit to a branch, even with a message like WIP: banner.

Common mistakes#

Practice#

Practice exercise

Add a line to pad.txt, stash it with the message draft, print the stash list, then restore it and print the file.

Show solution
Terminal
echo "two" >> pad.txt
git stash push -q -m "draft"
git stash list
git stash pop -q
cat pad.txt
Output
stash@{0}: On main: draft
one
two

Key takeaway#

Next: Tags and Releases.

Finished this lesson?