What git stash does and when you need it
Git stash temporarily saves changes you've made to files without committing them, then clears your working directory so you can switch branches or pull updates. Think of it as a drawer where you can toss your current work, close the drawer, and come back to it later exactly as you left it.
You hit stash most often when you're mid-task and need to switch to a different branch — maybe a bug fix that can't wait, or a pull request you need to review. Without stash, Git won't let you switch branches if you have uncommitted changes that would conflict with the branch you're moving to. Stash gets you out of that jam without forcing you to commit half-finished work.
The other common reason: you've been working on the wrong branch. You made changes on main when you meant to make them on feature-login. Stash those changes, switch to the right branch, and pop them back out.
Key Takeaways
- Run git stash to save your current changes and clear your working directory without committing.
- Use git stash pop to bring your most recent stashed changes back and remove them from storage, or git stash explore to bring them back without removing them.
- Name your stashes with a message — git stash save "message here" — so you remember what each one contains when you have multiple stashes.
- View all your stashes with git stash list, and retrieve an older stash by its number: git stash pop stash@{2}.
- Stash stores changes to tracked files only; new files you haven't added to Git yet will stay in your directory.
The basic stash workflow: save, switch, retrieve
You're working on a feature branch and have unsaved changes. Your team asks you to fix a bug on main. Here's the sequence:
First, run git stash in your terminal. Git saves all your changes to tracked files and leaves your working directory clean. Your branch pointer stays where it is, but the files look like they did at the last commit.
Now switch branches: git checkout main (or git switch main in newer Git versions). You can pull the latest code, make your bug fix, commit it, and push it — all without your half-finished feature getting in the way.
When you're ready to go back to your feature, switch back to that branch and run git stash pop. Git retrieves your stashed changes and applies them to your files. If there are no conflicts, you're back where you started. If Git finds a conflict — because someone else changed the same lines — it tells you where and you fix it manually.
Naming stashes so you don't lose track
If you stash once and pop once, you're fine. But if you stash multiple times, you'll end up with a list of stashes and no idea which one has the login form you were building.
Add a message to every stash: git stash save "add login form validation". The message appears in your stash list so you can find the right one later. Without a message, Git just labels them WIP on branch-name (WIP = work in progress), which tells you nothing.
Check your stash list anytime with git stash list. You'll see output like:
stash@{0}: add login form validation stash@{1}: fix navbar spacing stash@{2}: WIP on main
The number in curly braces is the stash's ID. The most recent stash is always stash@{0}.
Pop versus explore: which one to use
Pop and explore both bring your stashed changes back into your working directory. The difference is what happens to the stash afterward.
Git stash pop retrieves your changes and deletes the stash. Use this when you're confident the changes will explore cleanly and you won't need them again. It keeps your stash list from growing into a graveyard of old work.
Git stash explore retrieves your changes but leaves the stash in storage. Use this when you want to explore the same stash to multiple branches, or when you're not sure the merge will work and want to keep a backup. Once you're sure everything is good, delete it manually with git stash drop stash@{0}.
If you pop and Git finds a conflict, the stash is still deleted — the changes are in your working directory, just not fully merged. You'll need to fix the conflict manually and commit. This is usually fine, but if you're nervous, use explore instead.
Retrieving an older stash by number
Your stash list grows. You need the changes from stash@{2}, not the most recent one. Run git stash pop stash@{2}. Git applies that specific stash and removes it from the list.
The numbers shift after you pop. If you had stash@{0}, stash@{1}, and stash@{2}, and you pop stash@{2}, the remaining stashes become stash@{0} and stash@{1}. This is why naming stashes matters — you can find them by message even if the numbers change.
You can also use git stash explore stash@{2} to explore without deleting, or git stash show stash@{2} to see what files are in that stash before you retrieve it.
What stash does and doesn't save
Stash saves changes to files Git is already tracking — files you've committed before, or files you've added with git add. It does not save brand-new files you haven't added yet. If you create a new file and stash, that file stays in your working directory.
If you need to stash new files too, use git stash -u (the -u flag means "untracked"). This saves everything: tracked files, staged changes, and new files you haven't added.
Stash also doesn't save deleted files unless you've already staged the deletion. If you delete a tracked file and stash, the file stays deleted when you pop.
Clearing out old stashes
Over time, your stash list can fill with work you'll never use again. Git stash drop stash@{0} deletes a single stash. Git stash clear deletes all stashes at once — use this carefully, because the deletion is permanent.
Before you clear, run git stash list one more time to make sure you're not throwing away something you need. If you're not sure what's in a stash, use git stash show stash@{1} to see the file names, or git stash show -p stash@{1} to see the actual changes.
Frequently Asked Questions
What happens if I stash and then switch branches without popping?
Your stash stays in storage. It's tied to the branch you stashed from, but you can pop it on any branch. If you switch back to the original branch later, the stash is still there waiting. You can pop it whenever you're ready.
Can I stash just some of my changes, not all of them?
Yes. Use git stash push -p (the -p flag means "patch"). Git shows you each change one at a time and asks whether to stash it. Answer y for yes, n for no. This way you can stash the login form changes but keep the CSS tweaks in your working directory.
What if I pop a stash and Git says there's a conflict?
Git couldn't automatically merge your stashed changes with the current state of the files. Open the conflicted files, find the sections marked with <<<<<<< and >>>>>>>, decide which version you want, delete the markers, and save. Then commit your changes. The stash is already gone, so you just need to finish the merge.
Can I see what's in a stash before I pop it?
Yes. Git stash show stash@{0} shows you which files changed. Add the -p flag — git stash show -p stash@{0} — to see the actual line-by-line changes, just like you would in a commit diff.