Git Stash
You are halfway through cooking dinner when a neighbour rings the bell with an urgent request. You don't throw away the half-chopped vegetables. You put them in a covered bowl in the fridge, deal with the neighbour, and take the bowl out again when you come back.
git stash is that covered bowl. It takes your uncommitted changes, staged and unstaged, saves them in the repository, and gives you back a clean working tree. You can then switch branches, pull, or fix an urgent bug, and bring the saved changes back later. It is the answer to "I'm in the middle of something, but I need a clean folder right now", without making a half-finished commit.
In this lesson, you will stash work, inspect what you stashed, bring it back in the right way, and handle untracked files and longer-lived stashes.
The Starting Point
Navika is in the middle of adding the hostel fee. She has edited fees.txt (not staged), staged a change to README.md, and made a new file of notes:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
printf 'Term 1 fee: $1,200\nLate fee: $250\n' > fees.txt
git add .
git commit --quiet -m "Add README and fees"
echo 'Hostel fee: $9,000' >> fees.txt
echo "Includes the hostel fee." >> README.md
git add README.md
echo "ask about hostel discount" > notes.txt
git status -sM README.md
M fees.txt
?? notes.txtThen the school office calls: the term 1 fee shows $1,200 instead of $12,000, and it must be fixed now. Her half-done work shouldn't go into that fix.
Stash Your Work
git stash push -m "Half-done hostel fee"
git status -sSaved working directory and index state On main: Half-done hostel fee
?? notes.txtSaved working directory and index state means both the unstaged change and the staged change were saved, and removed from the folder. On main is the branch, and the text after it is the message.
Look at what is left: notes.txt. Untracked files are not stashed by default. They stay in the folder. Usually that is fine, and the section below shows how to include them when it isn't.
git stash with no arguments does the same, with a message made from the last commit. Always add -m: three weeks later, "WIP on main" tells you nothing.
See What Is Stashed
git stash list
git stash showstash@{0}: On main: Half-done hostel fee
README.md | 1 +
fees.txt | 1 +
2 files changed, 2 insertions(+)stash@{0} is the newest stash. Stashes form a stack: the next one becomes stash@{0}, and this one moves to stash@{1}. git stash show summarizes the changes, and -p shows the full diff:
git stash show -p stash@{0}diff --git a/README.md b/README.md
index d5924b0..e53bec0 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
Fee Tracker
+Includes the hostel fee.
diff --git a/fees.txt b/fees.txt
index 44d1fe9..f61cb6d 100644
--- a/fees.txt
+++ b/fees.txt
@@ -1,2 +1,3 @@
Term 1 fee: $1,200
Late fee: $250
+Hostel fee: $9,000Fix the Urgent Bug
The folder is clean apart from the notes, so Navika fixes the fee and commits:
sed -i.bak 's/\$1,200$/$12,000/' fees.txt
rm fees.txt.bak
git commit --quiet -am "Fix the term 1 fee"
git log --onelinec5a3eb7 (HEAD -> main) Fix the term 1 fee
0ef7091 Add README and feesBring the Work Back: pop
git stash pop applies the newest stash and, if that worked, removes it from the list. --index also restores what was staged:
git stash pop --indexAuto-merging fees.txt
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
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: fees.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
notes.txt
Dropped refs/stash@{0} (10c0d027c321bc35525dc5f637383255a69938fa)Git applied the stash on top of the new commit, shows the status, and dropped the stash: Dropped refs/stash@{0}. Both the fix and the hostel work are in fees.txt now, and README.md is staged again, as before:
cat fees.txt
git status -sTerm 1 fee: $12,000
Late fee: $250
Hostel fee: $9,000
M README.md
M fees.txt
?? notes.txtWithout --index, Git would have put back all the changes, but none of them staged. That is often fine. Add --index when the staging matters.
apply vs pop
| Command | Applies the stash | Removes it from the list |
|---|---|---|
git stash pop | Yes | Yes, if it applied without conflicts |
git stash apply | Yes | No. You drop it yourself with git stash drop |
Use apply when you want to apply the same stash to more than one branch, or want to keep it as a backup until you are sure. If a pop runs into a conflict, Git applies what it can, marks the conflict as in a merge, and keeps the stash, so nothing is lost. Resolve the conflict, git add the file, then git stash drop.
Include Untracked Files: -u
Navika now wants to put everything aside, including notes.txt. -u (--include-untracked) stashes untracked files too:
git stash push -u -m "Hostel fee with notes"
git status -s
git stash listSaved working directory and index state On main: Hostel fee with notes
stash@{0}: On main: Hostel fee with notesThe folder is completely clean. -a (--all) would also stash ignored files, such as build output, which is rarely what you want.
Turn a Stash into a Branch
When stashed work turns out to be bigger than expected, it deserves its own branch. git stash branch creates a branch at the commit where the stash was made, applies the stash there (so it never conflicts), and drops it:
git stash branch feature/hostel-feeSwitched to a new branch 'feature/hostel-fee'
On branch feature/hostel-fee
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
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: fees.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
notes.txt
Dropped refs/stash@{0} (7a42ef96b57664cb9bb0b1335165f1e6427b2a54)git branch --show-current
git status -sfeature/hostel-fee
M README.md
M fees.txt
?? notes.txtNavika is on feature/hostel-fee with all her work, including the notes, ready to commit there.
Syntax
git stash push [-m "<message>"] [-u | -a] [<path>...]
git stash list | show [-p] | apply | pop | drop | branch | clear| Command | It is used to |
|---|---|
git stash push -m "<message>" | Save staged and unstaged changes, and clean the folder |
git stash push -u | Also save untracked files |
git stash push <path> | Stash only some files |
git stash list | List stashes |
git stash show -p stash@{n} | Show a stash's changes |
git stash pop [--index] | Apply the newest stash and remove it |
git stash apply [stash@{n}] | Apply a stash and keep it |
git stash drop [stash@{n}] | Delete a stash |
git stash branch <name> [stash@{n}] | Create a branch from a stash |
git stash clear | Delete all stashes |
Common Mistakes
Expecting new files to be stashed. Plain git stash leaves untracked files in the folder. Use -u.
Collecting stashes for weeks. A list of ten "WIP on main" entries is a mystery. Use messages, and turn longer work into a branch with commits.
git stash clear or drop by mistake. The stash is gone from the list, but its commit exists for a while. The lessons on recovering lost work show how to find it.
Popping onto the wrong branch. A stash applies to whatever branch you are on. Check git branch --show-current first.
Using stash instead of commits for real work. A stash isn't pushed, isn't backed up and is easy to forget. For anything you would hate to lose, commit on a branch, even with a "WIP" message, and clean it up later.
Under the Hood
A stash is stored as ordinary commits. refs/stash points to the newest one, and the stash list is that ref's reflog. Each stash is a commit whose parents are the commit you were on, a commit holding the staging area, and, with -u, a commit holding the untracked files. To see one, Navika first commits her hostel work, then stashes a small draft line:
git add .
git commit --quiet -m "Add the hostel fee"
echo "Draft: discount rules" >> README.md
git stash push --quiet -m "Show how a stash is stored"
git log --oneline --graph -3 stash* 00794f2 (refs/stash) On feature/hostel-fee: Show how a stash is stored
|\
| * 380d0e1 index on feature/hostel-fee: 8d3e581 Add the hostel fee
|/
* 8d3e581 (HEAD -> feature/hostel-fee) Add the hostel feeThe top commit is the stash itself, with the working tree changes. index on feature/hostel-fee holds what was staged. Both sit on top of the commit the stash was made on, "Add the hostel fee". That is why stashes can be shown with git show, compared with git diff, and recovered like commits.
Interview Corner
What does git stash do?
It saves uncommitted changes, staged and unstaged, in the repository as a stash entry, and resets the working tree and staging area to HEAD. You can restore them later with git stash pop or git stash apply.
What is the difference between git stash pop and git stash apply?
Both apply a stash to the current branch. pop then removes it from the stash list (unless there was a conflict). apply keeps it, so you can apply it again or drop it later.
How do you stash untracked files?
git stash push -u (or --include-untracked). -a (--all) also includes ignored files.
How do you restore staged changes as staged?
git stash pop --index (or apply --index). Without --index, all changes come back unstaged.
How is a stash stored internally?
As a commit (with the working tree state) whose parents are the commit it was made on and a commit holding the index, plus a third parent with untracked files when -u is used. refs/stash points to the newest, and the list is its reflog.
Practice
Challenge: what stays behind? On feature/hostel-fee, edit README.md by adding Run the app., create a new file todo.txt, and run plain git stash push -m "Readme and todo". Print the short status and the stash list.
What does Git print? Before you open the solution, decide which file is left in the folder after the stash, and how many stashes are listed.
Show solution
echo "Run the app." >> README.md
echo "call the office" > todo.txt
git stash push -m "Readme and todo"
git status -s
git stash listSaved working directory and index state On feature/hostel-fee: Readme and todo
?? todo.txt
stash@{0}: On feature/hostel-fee: Readme and todo
stash@{1}: On feature/hostel-fee: Show how a stash is storedtodo.txt stays, because plain git stash doesn't take untracked files. Two stashes are listed: the new one as stash@{0}, and the one from "Under the Hood", now stash@{1}.
Checkpoint. In a practice repository, start a change, stash it with a message, fix something else and commit it, then pop the stash. Repeat once with an untracked file and -u, and once turning the stash into a branch.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git stash push -m "<message>" | Stash with a message |
git stash push -u | Include untracked files |
git stash list | List stashes |
git stash show -p | Show the newest stash's diff |
git stash pop --index | Restore and remove, keeping staging |
git stash apply stash@{1} | Restore an older stash, keep it |
git stash drop stash@{1} | Delete one stash |
git stash branch <name> | Move a stash onto a new branch |
Was this tutorial helpful?