git stash
Saving uncommitted changes temporarily with git stash — switching contexts without losing work.
What you'll learn
- How to stash and pop changes to switch contexts quickly
- Stashing specific files and with custom messages
- Listing, applying, and dropping stashes
- The difference between stash pop and stash apply
Concept
What git stash Does
git stash saves your uncommitted changes (staged and unstaged) in a temporary stack, then restores your working directory to a clean state. This lets you switch branches or pull updates without committing half-finished work.
git stash
After stashing, git status shows a clean working tree. Your changes are safely stored and can be retrieved later.
Popping Stashes
To retrieve your stashed changes, use git stash pop:
git stash pop
This applies the most recent stash and removes it from the stash stack. If there are conflicts, the stash is kept so you can resolve them and drop it manually.
Stash vs Apply
git stash apply # apply the stash but KEEP it in the stack
git stash pop # apply the stash and REMOVE it from the stack
Use apply if you want to apply the same stash to multiple branches. Use pop when you are done with the stash.
Stashing with a Message
By default, stashes are described as WIP on branchname. You can add a descriptive message:
git stash push -m "work in progress on login form validation"
This makes it much easier to find the right stash later when you have multiple stashed changes.
Listing and Managing Stashes
git stash list # see all stashes
git stash show -p # see what is in the latest stash
git stash show stash@{1} # see a specific stash
git stash drop # discard the latest stash
git stash drop stash@{1} # discard a specific stash
git stash clear # discard ALL stashes (dangerous!)
Stashes are referenced as stash@{0} (most recent), stash@{1} (second most recent), and so on.
Stashing Specific Files
# Stash only specific files
git stash push -m "layout changes" layout.css header.js
# Stash only staged changes
git stash --staged
# Stash including untracked files
git stash -u
The Typical Workflow
# You are working on a feature but need to switch context
git stash push -m "half-finished login validation"
# Switch to main and fix an urgent bug
git switch main
git switch -c hotfix-urgent
# ... fix the bug, commit, merge ...
# Come back to your feature
git switch feature-login
git stash pop
# Your changes are back!
Stashing is the Git equivalent of "let me put this aside for a moment".
Example
# You have uncommitted changes but need to switch branches
echo "half-done work" >> login.js
git status # shows uncommitted changes
# Stash your work with a descriptive message
git stash push -m "login form validation in progress"
# Working tree is now clean — switch branches safely
git status # nothing to commit, working tree clean
git switch main
git pull
# Do something else (fix a bug, etc.)
git switch -c hotfix-bug
echo "fix" > fix.js
git add fix.js
git commit -m "fix: urgent bug"
git switch main
git merge hotfix-bug
# Come back and restore your stashed work
git switch feature-login
git stash list # see your stashes
git stash pop # restore the changes
# Stash only specific files
git stash push -m "only CSS" style.css
# Stash including untracked (new) files
git stash -u
# Apply a specific stash without removing it
git stash apply stash@{1}
# Drop a stash you no longer need
git stash drop stash@{0}
# See what is in a stash before applying
git stash show -p stash@{0}
Shows the full stash workflow: stashing with a message, switching context, coming back and popping, stashing specific files, stashing untracked files, applying without popping, and viewing stash contents. The descriptive message makes it easy to find the right stash later.
Try it
- Code Diff
Use git stash show -p to see stash contents as a diff.
- .gitignore Generator
Keep your workspace clean so you stash less often.
Common mistakes
The mistake
Using git stash clear and losing stashed work permanently
The fix
git stash clear deletes ALL stashes with no confirmation. There is no easy undo. Instead, use git stash drop stash@{n} to remove specific stashes one at a time, and always verify with git stash list first.
The mistake
Forgetting about a stash and leaving work stranded for days
The fix
Always use git stash push -m 'description' so you can identify stashes later. Run git stash list periodically to check for forgotten stashes. Stashes are not pushed to remotes, so they only exist on your machine — do not rely on them for long-term storage.
Related Resources
Related Cheatsheets
- Git Cheatsheet
stash, stash pop, stash list, and stash drop commands.
Practice git stash
7 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.