git stash
Put work aside with stash
Lesson 18 of 20 · 9 min · Module 5: Teamwork and fixing mistakes
You are in the middle of something when an urgent fix comes in. You do not want to commit half-done work, and you do not want to lose it. Stash it.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Stash uncommitted work, including new files
- Make an urgent fix on a clean tree
- Bring stashed work back
The idea
git stash saves your uncommitted changes and leaves a clean working tree. New, untracked files stay put unless you add -u. git stash list shows saved stashes, and git stash pop brings the latest one back and removes it from the list. git stash push -m "message" gives a stash a name.
git stash -u
git stash list
git stash pop
01Stash: a shelf for unfinished work
git stash saves your uncommitted changes and gives you a clean working tree. With -u, new files go on the shelf too.
git stash -u
git stash list
git status --shortSaved working directory and index state WIP on main: 6688add Add a blog to the menu
stash@{0}: WIP on main: 6688add Add a blog to the menu02Fix, commit, come back
With a clean tree you can make an urgent fix, or switch branches. git stash pop then brings the latest stash back and removes it from the list.
printf 'Home\nAbout\nBlog\n' > menu.txt
git commit -am "Fix a typo in the menu"
git stash pop[main c3fb643] Fix a typo in the menu
1 file changed, 1 insertion(+), 1 deletion(-)
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: style.css
Untracked files:
(use "git add <file>..." to include in what will be committed)
draft.md
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (68e65b680e6691e163bfc64dc689ac86f321a0b2)03Name your stashes
git stash push -m "redesign" stores a message with the stash. With several stashes, git stash list shows them as stash@{0}, stash@{1} and so on, and git stash pop stash@{1} picks a specific one.
Your turn
Abuot. Put your unfinished work aside with git stash -u, so the new file draft.md is included. Fix the menu so it reads Home, About, Blog, and commit only that as Fix a typo in the menu. Then bring your redesign back with git stash pop.What the checks look for
- The typo fix is committed on its own
- Your redesign is back
- The stash list is empty again
- Stashes the new file too
Take this with you
git stash -u puts work aside; git stash pop brings it back.
Go a little further
Stash twice with different messages using git stash push -m, then look at git stash list and git stash show -p stash@{1}.