Git stash temporarily shelves your uncommitted changes so you can switch branches, pull updates, or run tests on a clean working tree without losing work.
It saves modified files (and optionally untracked/ignored files) into a stack like storage, letting you reapply them later via git stash apply or remove and apply in one step with git stash pop.
If you’ve ever needed to pause what you’re doing, switch tasks, and return without losing progress, the git stash command is your best friend.
In this guide, I’ll explain the Git stash command with simple examples, pro tips, and common pitfalls based on real workflows used in hosting, CI/CD, and collaborative teams.
What is Git Stash and How Does it Work?
Git stash takes your dirty working directory (changes not yet committed) and stores those changes in a special stack referenced by refs/stash. Your working directory is then cleaned to match HEAD, letting you switch branches or pull without conflicts. Later, you can reapply the stashed changes to any branch.
What Gets Stashed?
- Tracked changes: Modified files that Git already tracks.
- Untracked files: Only if you use
--include-untrackedor-u/-a. - Ignored files: Only if you use
--all. - Index vs working tree: You can control whether staged changes are kept staged using
--keep index.
When to Use Git Stash
- You need to switch branches but have local edits you’re not ready to commit.
- You want to pull/rebase cleanly without committing partial work.
- You’re experimenting and want a safe temporary snapshot.
- Your CI/CD or staging workflow requires a clean working tree to test a build.
Quick Start: The Most Useful Git Stash Commands
Create a Stash (save/push)
Modern Git uses git stash push (equivalent to git stash in many cases). Add a message so you recognize it later.
# Stash tracked changes with a message
git stash push -m "WIP: homepage layout tweak"
# Include untracked files (e.g., new files)
git stash push -u -m "WIP: add ContactForm component"
# Include ignored files too (rare)
git stash push -a -m "Temp build outputs stashed"
See Your Stashes
git stash list
git stash show -p stash@{0} # show patch of a specific stash
Reapply vs Reapply and Remove
- Apply replays changes but keeps the stash:
git stash apply - Pop replays changes and removes the stash:
git stash pop
# Apply the most recent stash
git stash apply
# Apply a specific stash
git stash apply stash@{2}
# Pop (apply and remove)
git stash pop
git stash pop stash@{1}
Clean Up Stashes
# Remove one stash
git stash drop stash@{0}
# Remove all stashes
git stash clear
Create a Branch From a Stash
# Great for turning WIP into a feature branch
git stash branch feature/new-ui stash@{0}
Practical Git Stash Examples (Step-by-Step)
1) Stash Local Edits to Pull the Latest Code
# You have uncommitted changes on main
git stash push -m "WIP: optimize hero image"
# Get the latest changes safely
git pull --rebase
# Reapply your work
git stash pop
Keeps your history clean and reduces merge conflicts by rebasing onto fresh upstream commits.
2) Stash Only Certain Files
# Only stash changes in src/components
git stash push -m "WIP: components only" -- src/components
# Only stash a single file
git stash push -m "WIP: header styles" -- src/styles/header.css
Pathspecs let you limit the scope of your stash, perfect for large repos where unrelated changes shouldn’t be touched.
3) Stash Untracked or Ignored Files
# Include untracked files
git stash push -u -m "WIP: new config files"
# Include everything (tracked, untracked, ignored)
git stash push -a -m "WIP: stash node_modules & build"
Use -a cautiously. Stashing large ignored folders (like node_modules/) can be slow and is rarely necessary.
4) Resolve Conflicts After Applying a Stash
If apply or pop results in conflicts, Git marks files just like a merge conflict. Fix them, then mark as resolved.
# After conflicts occur
git status
# Edit files and resolve conflict markers
git add <resolved-files>
# If you used pop and conflicts happened, the stash may still exist until resolution completes
5) Convert a Stash Into a Feature Branch
Turn work in progress into its own branch without affecting your current one.
git stash branch feature/login-ui stash@{0}
# Now commit changes on the new branch
Advanced Git Stash Techniques
Add a Descriptive Message
# Always describe your stash for future you (or teammates)
git stash push -m "WIP: refactor API client for retries"
Keep Staged Changes Staged
Use --keep-index to stash only unstaged changes. Perfect when your commit is half ready but you need to pause.
# Keeps staged changes intact; stashes only unstaged edits
git stash push --keep-index -m "Pause: tests failing, keep staged fix"
Stash Interactively (Patch Mode)
Choose specific hunks to stash, useful when a file contains both ready to commit and still experimental parts.
git stash push -p -m "WIP: stash only parts of file"
Compare Apply vs Pop vs Branch
- apply: Safest when you’re not sure it’ll cleanly reapply (the stash remains if conflicts occur).
- pop: Faster when confident, applies and removes the stash in one step.
- branch: Best for isolating WIP into its own branch for code review or CI.
Common Mistakes and How to Avoid Them
- Forgetting what’s in a stash: Always use
-mmessages andgit stash show -pbefore applying. - Using pop when conflicts are likely: Prefer
apply, verify the result, then manuallydrop. - Stashing massive ignored folders: Avoid
-aunless essential. It slows down operations and can bloat your repo metadata. - Relying on stash as a long term storage: Stash is for short term task switching. Convert to a branch when work extends beyond a day.
Real World Workflow: Teams, CI/CD, and Hosting
In fast moving teams, you’ll often stash to run quick hotfixes or pull updates before a release. Combined with code review and CI, stash keeps your working copy clean without polluting history with “WIP” commits.
- Before rebasing:
git stash push,git pull --rebase, thengit stash pop. - Before urgent hotfix: Stash your current work, checkout hotfix branch, patch, deploy, then return and pop.
- For staging/testing: Keep WIP out of the staging branch; convert critical stashes into a feature branch (
git stash branch ...) for CI tests.
If you use managed hosting with Git workflows, look for staging environments that support branch based deployments. At YouStable, our developer friendly hosting makes it easy to spin up staging sites per branch, so you can turn a git stash into a proper feature branch and test changes safely, without risking production.
Troubleshooting Git Stash
“Where Did My Stash Go?”
Run git stash list. If you accidentally dropped a stash, check the reflog. Stash entries are commits; sometimes you can recover them.
# Show stash stack
git stash list
# Inspect HEAD and stash movements
git reflog
# If you spot the lost commit hash, create a branch to recover
git branch rescue <commit-hash>
Applied the Wrong Stash
Use git reset --hard to revert your working tree to a known commit (careful: discards uncommitted changes). Or commit the unintended state and revert via a new commit, safer on shared branches.
Stash Conflicts on Apply/Pop
- Inspect conflicts:
git status - Resolve markers in files:
<<<<<<<,=======,>>>>>>> - Mark resolved:
git add - Complete the apply: if using
pop, ensure it finished; otherwise re-apply carefully
Best Practices for Using Git Stash
- Prefer small, frequent commits for shareable work; use stash for short, private context switches.
- Name your stashes with
-mfor clarity during busy sprints. - Use apply before pop when you anticipate conflicts or are reapplying across branches.
- Convert long lived stashes into feature branches:
git stash branch. - Keep the stack tidy: drop or clear stashes you no longer need.
Git Stash Command Reference (Cheat Sheet)
# Save changes
git stash push -m "message"
git stash push -u -m "include untracked"
git stash push -a -m "include ignored"
# Inspect
git stash list
git stash show -p stash@{n}
# Reapply
git stash apply [stash@{n}]
git stash pop [stash@{n}]
# Remove
git stash drop [stash@{n}]
git stash clear
# Branch from stash
git stash branch <branch-name> [stash@{n}]
# Partial/interactive
git stash push -p -m "partial stash"
# Keep staged changes
git stash push --keep-index -m "keep index"
FAQ’s
1. What does git stash actually store and where?
Each stash is a set of commits stored under refs/stash. It typically includes a commit for the index (staged changes) and working tree (unstaged changes). You can view them with git stash list and inspect contents via git stash show -p.
2. What’s the difference between git stash apply and pop?
apply reapplies changes but keeps the stash so you can try again or use it elsewhere. pop reapplies changes and removes the stash entry. If conflicts are likely, use apply first, then drop after success.
3. How do I stash untracked files or ignored files?
Use -u/--include-untracked to stash untracked files, and -a/--all to include ignored files too. Example: git stash push -u -m "stash new files" or git stash push -a -m "stash everything".
4. Can I stash only specific files or parts of a file?
Yes. Limit by path: git stash push -- path/to/file. For partial changes, use interactive mode: git stash push -p to select hunks line by line.
5. Is git stash safe for long-term storage?
Stash is designed for short-term task switching. For longer work, convert a stash into a branch using git stash branch <name>. That way, CI, code review, and collaboration workflows can proceed normally.
Conclusion
Git stash is a powerful safety net for developers: it preserves momentum, reduces merge pain, and keeps your history clean. Use messages, apply before pop when unsure, and branch off stashes for ongoing work.
Building and testing on staging branches like those supported on YouStable’s Git friendly hosting, helps you ship confidently without losing a line of code.