git stash: Save and Restore Unfinished Work
Set aside changes without committing them, switch context, and come back to exactly where you left off.
You are in the middle of something when an urgent bug report comes in on another branch. Your changes are not ready to commit. git stash gets you out of this situation in one command.
Stash your current changes
git stash
This takes everything in your working tree and index that is not in the last commit and saves it to a stack. Your working directory goes back to a clean state matching HEAD.
By default, git stash includes:
- Modified tracked files
- Staged changes
It does not include untracked files or ignored files unless you ask for them explicitly.
Bring the changes back
git stash pop
pop applies the most recent stash and removes it from the stack. If you want to apply without removing:
git stash apply
apply is useful when you want to apply the same stash to multiple branches.
Name your stashes
A bare git stash creates entries with names like stash@{0}: WIP on main: a1b2c3d add login form. After a few stashes, those names are useless. Add a message:
git stash push -m "half-finished refactor of auth middleware"
List stashes:
git stash list
stash@{0}: On main: half-finished refactor of auth middleware
stash@{1}: WIP on feature/payments: e4f5g6h update cart total
Apply a specific stash by index:
git stash apply stash@{1}
Include untracked files
New files you have created but not yet staged are not stashed by default:
git stash push -u
-u (or --include-untracked) includes untracked files. New files disappear from your working directory and come back with pop or apply.
To also stash ignored files:
git stash push -a
Be careful with -a — it can stash build output, local caches, and other generated files you probably did not mean to save.
Stash only specific files
git stash push path/to/file.js path/to/other.js
Or use the interactive mode to pick hunks:
git stash push -p
-p (patch mode) shows each change individually and asks whether to stash it. Same interface as git add -p.
Create a branch from a stash
If the context has moved on and applying the stash directly causes conflicts:
git stash branch new-branch stash@{0}
This creates a new branch from the commit where the stash was originally created, applies the stash to it, and drops the stash entry. Conflicts disappear because the branch starts from the exact point where the stash was made.
Drop stashes you no longer need
git stash drop stash@{1} # drop a specific entry
git stash clear # remove all stashes
Stashes are not garbage-collected automatically. If you accumulate months of old stashes, git stash list becomes noise and git stash clear is the cleanest solution.
When stash is not the right tool
Stash is for short interruptions — a few minutes to a few hours. For work that will sit dormant for days, a commit on a branch is better. A git commit -m "WIP" on a feature branch is more visible, survives machine failures, and can be pushed to a remote. You can always git commit --amend or use an interactive rebase to clean up the WIP commit before the branch is reviewed.
SysEmperor