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.
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#
echo "Half-finished banner" >> index.html
git status --short
git stash
git status --short M index.html
Saved working directory and index state WIP on main: 4e04c7b Add home pageThe edit is gone from the file, and the working tree is clean. Do the urgent work, then bring it back with git stash pop:
git stash popOn 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:
git stash push -q -m "banner work"
echo "Trying a new font" >> index.html
git stash push -q -m "font experiment"
git stash liststash@{0}: On main: font experiment
stash@{1}: On main: banner workStashes form a stack: stash@{0} is the newest. Restore a particular one by number:
git stash apply stash@{1}
git stash listOn 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 workSee what is inside#
git stash show -p stash@{0}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 fontWithout -p you get a summary of the files.
Untracked files#
By default, git stash ignores new files. Add -u to include them:
git stash clear
echo "New page" > new.html
git stash
git status --shortNo local changes to save
?? new.htmlgit stash -u
git status --short
git stash listSaved working directory and index state WIP on main: 4e04c7b Add home page
stash@{0}: WIP on main: 4e04c7b Add home pageNo 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:
git stash -q -u
git stash branch new-pageSwitched 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
echo "two" >> pad.txt
git stash push -q -m "draft"
git stash list
git stash pop -q
cat pad.txtstash@{0}: On main: draft
one
twoKey takeaway#
Next: Tags and Releases.
Finished this lesson?