You are halfway through a change on a branch, code is incomplete, not ready for a commit. Then an urgent request arrives: a bug to fix, a client request, a PR to review. What do you do? Delete everything? Make a dirty commit that pollutes history? No, you use git stash.
We, at Meteora Web, juggle multiple development fronts every day. When an emergency hits while we're in the middle of a feature, stash is our lifeline. This guide covers how to use it safely, when it's the best tool, and when to choose a branch instead.
Why use git stash when an interruption arrives?
Imagine you have modified 5 files on a development branch. If you try git checkout to switch to another branch, Git blocks you: “Your local changes would be overwritten by checkout”. You have two options: commit incomplete work (with a messy “WIP” message) or lose your changes. Both are painful.
Stash solves the problem: it saves uncommitted changes into a temporary stack, cleans your working directory, and gives them back when you want. It's like pausing work without closing it.
Sponsored Protocol
We use it daily to:
- Switch to an urgent hotfix on another branch.
- Help a colleague with a PR while we have local changes.
- Try a “dirty” idea without polluting Git history.
It's fast, reversible, and keeps your commit log clean.
How does git stash work and what are the basic commands?
git stash automatically saves modifications to tracked files (already added with git add or modified) and restores the working directory to the last commit state. To include new untracked files, use git stash -u (or --include-untracked).
Core command: git stash
# Save current changes (tracked)
git stash
# Also save untracked files
git stash -u
# Save with a descriptive message (recommended!)
git stash push -m "WIP: refactoring login module"
git stash push -m "message" is the modern approach that lets you label each stash — essential when you accumulate more than one.
List all stashes
git stash list
# Typical output:
# stash@{0}: On develop: WIP refactoring login module
# stash@{1}: On feature-x: temporary API fix
Each stash has a reference stash@{n} and a message. More stashes mean more clutter. We recommend keeping no more than 3-4 active stashes to avoid confusion.
Sponsored Protocol
Retrieve changes
Two approaches:
git stash pop— applies the most recent stash and removes it from the list.git stash apply— applies the stash but keeps it in the list (useful if you want to apply it to multiple branches).
# Apply and remove the most recent stash
git stash pop
# Apply a specific stash
git stash apply stash@{1}
# Apply without removing
git stash apply
Caution:
git stash popcan cause conflicts. If you resolve them badly, the stash is still removed. Better to usegit stash applyfirst, verify, thengit stash dropmanually.
How to retrieve or delete a stash safely?
You can inspect a stash before applying it:
# List files changed in stash
git stash show stash@{0}
# Show full diff
git stash show -p stash@{0}
To remove a stash you no longer need:
# Delete most recent stash
git stash drop
# Delete a specific stash
git stash drop stash@{2}
# Clear all stashes
git stash clear
Operational tip: after weeks of development, run git stash list and discard stashes you don't need. We do this every Friday.
Sponsored Protocol
How to handle conflicts when applying a stash?
If the branch where you apply the stash has changes that conflict with the point where you created the stash, Git reports:
Auto-merging file.php
CONFLICT (content): Merge conflict in file.php
You then need to:
- Open the conflicting files.
- Resolve manually (accept yours, theirs, or a mix).
git addand then eithergit stash drop(if you want to remove it) or leave it.
We recommend applying stash always from a clean branch (after committing or stashing everything else) to reduce conflicts. Still, knowing how to resolve merge conflicts is essential: dive deeper in our Git for Developers guide.
When to avoid stash and use a branch instead?
Stash is perfect for short, temporary interruptions (minutes, a few hours). If the priority shift lasts days or weeks, create a branch. A branch gives you:
- Traceability in history.
- Ability to share with the team.
- Automatic backup to remote.
Also avoid stash when you have uncommitted changes to sensitive config files (e.g., .env) — better to .gitignore them and commit separately.
Sponsored Protocol
Another case: stash on large binary files can be slow and space-consuming. Prefer a temporary commit on a local branch.
At Meteora Web, we follow a clear rule:
- Interruption < 1 hour → stash.
- Interruption > 1 hour or long context switch → branch with a local “WIP” commit, later squashed.
What to do next
- Open a terminal in a Git repository (even a test project).
- Modify a file, then run
git statusto see the changes. - Execute
git stash push -m "test first stash". - Switch branch with
git checkout main— it works without errors. - Go back to your original branch:
git checkout -and thengit stash pop. - Verify your changes are back.
Practice with multiple stashes, -u, apply, drop, and clear. Learning stash today will save you from losing code or making dirty commits.
Want to master the full Git workflow for your team? Read our complete Git for Developers guide.