Git Stash: Save Your Work Without Committing

If you've ever wondered "what is git stash", "what does git stash do", or looked for a simple git stash tutorial, this guide will explain everything in clear and practical terms. While working on a project, sometimes you need to switch branches or pull new changes, but your work is not ready to commit yet. This is where Git Stash becomes very useful.
Git stash lets you temporarily save your uncommitted changes, clean your working directory, and then bring those changes back later.
In this guide you will learn what stash in Git is, every command you need with examples, a realistic scenario you can follow along with, the mistakes that trip up beginners, and answers to frequently asked questions.
New to Git? Our guide to the 10 basic Git commands every beginner should know covers the foundations used in this tutorial.
What is Git Stash?
git stash takes your modified files (staged and unstaged) and stores them in a safe place. Your working directory becomes clean, just like the last commit.
You can think of stash as a clipboard for your code. You "cut" your unfinished work, do something else, and "paste" it back whenever you are ready.
A few important facts about how stash works:
It is a stack. The newest stash sits on top as
stash@{0}, the one before it isstash@{1}, and so on.It is local. Stashes live only in your own repository. They are not pushed to GitHub or GitLab, and teammates cannot see them.
It works across branches. You can stash on one branch and apply the changes on another.
By default it saves tracked files only. Modified and staged files are stashed, but brand-new untracked files are left behind unless you add a flag (covered below).
Why Not Just Commit?
You might wonder why you cannot simply make a quick commit. You can, but a half-finished commit with a message like "WIP" clutters your history and may break the build. Stash keeps your history clean.
Option | Best for | Downside |
|---|---|---|
| Quick, temporary context switch | Local only, easy to forget about |
WIP commit | Work you want backed up on a branch | Messy history unless you clean it up later |
New branch | Longer-running side work | More overhead for a small interruption |
Basic Git Stash Commands
1. Stash your changes
This saves all your uncommitted changes to tracked files and resets your working directory.
git stashSample output:
Saved working directory and index state WIP on main: a1b2c3d Add homepagegit stash is shorthand for git stash push.
2. Stash with a message
Adding a message helps you remember why you stashed the changes. This is a habit worth building, because plain stashes all look identical in the list.
git stash push -m "work in progress on login page"You may see older tutorials use git stash save "message". That form is deprecated, so use git stash push -m instead.
3. View all stashes
git stash listExample output:
stash@{0}: On main: work in progress on login page
stash@{1}: On feature: navbar UI updateTo see what a stash contains without applying it:
git stash show stash@{0}
git stash show -p stash@{0}The first command shows a summary of changed files, and -p shows the full diff.
4. Apply the latest stash
This restores the changes but keeps the stash in the list.
git stash applyUse apply when you want to reuse the same changes on more than one branch, or when you want a safety copy until you are sure everything works.
5. Apply and remove the stash
This restores the changes and deletes the stash after applying.
git stash popIf applying the stash causes a merge conflict, Git keeps the stash in the list so you do not lose it. Resolve the conflict, then drop the stash manually once you are happy.
6. Apply a specific stash
git stash apply stash@{1}You can use git stash pop stash@{1} the same way.
Tip for some shells: in PowerShell, and occasionally in other shells, the curly braces need quotes: git stash apply "stash@{1}".
7. Drop a stash
This deletes a specific stash.
git stash drop stash@{0}8. Clear all stashes
This removes all stashed changes permanently.
git stash clearBe careful here. Check git stash list first, because clearing is not easy to undo.
Command Cheat Sheet
Command | What it does |
|---|---|
| Stash tracked changes |
| Stash with a descriptive message |
| List all stashes |
| Show the diff of the latest stash |
| Restore the latest stash and keep it |
| Restore the latest stash and delete it |
| Restore a specific stash |
| Delete one stash |
| Delete all stashes |
Useful Stash Options
Include untracked files
New files that Git is not yet tracking are skipped by default. To include them:
git stash push -u -m "include new files"-u is short for --include-untracked. Use -a (--all) to also include files ignored by .gitignore, which is rarely needed.
Stash only specific files
git stash push -m "only the navbar" src/navbar.css src/navbar.jsChoose changes interactively
git stash push -pGit walks you through each change and asks whether to stash it, similar to git add -p.
Keep staged changes in place
git stash push --keep-indexThis stashes your work but leaves staged changes in the working directory. It is handy for testing exactly what you are about to commit.
Restore staged state too
git stash apply --indexBy default, changes that were staged come back as unstaged. Add --index to restore them as staged.
Turn a stash into a branch
git stash branch new-branch-name stash@{0}This creates a branch from the commit where you stashed, applies the stash, and drops it if it applied cleanly. It is a great fix when the stash no longer applies cleanly to your current branch.
Example Usage Scenario
Imagine you are working on a feature and suddenly your manager asks you to fix a production bug.
Step 1: Save your current work
git stash push -m "halfway login form"Your working directory is now clean.
Step 2: Switch branch and fix the bug
git checkout mainFix the bug, commit it, and push it as you normally would. (On newer Git versions, git switch main does the same thing.)
Step 3: Come back and restore your work
git checkout feature/login
git stash popYour unfinished work is back exactly as it was.
Another Common Scenario: Pulling When You Have Local Changes
If you try to pull while you have uncommitted changes that overlap with incoming ones, Git may refuse. Stash makes this easy:
git stash
git pull
git stash popGit also has a shortcut that does the same thing in one command:
git pull --autostashWhen Should You Use Git Stash?
When you need to quickly switch branches
When your work is not ready to commit
When pulling new changes causes conflicts
When you want to keep your commit history clean
When you want to test something on a clean working directory and then return to your changes
Common Problems and How to Fix Them
"I can't find my new file after stashing"
Plain git stash leaves untracked files in place, so a new file is not stashed (and stays visible). If you wanted it included, use git stash push -u.
"Stash pop caused a conflict"
Open the conflicted files, fix the marked sections, and stage them with git add. Because Git keeps the stash after a conflict, run git stash drop once you are satisfied the changes are correct.
"I dropped or cleared a stash by mistake"
You may still be able to recover it for a while. Run git fsck --no-reflog | grep "dangling commit" to list unreachable commits, then inspect candidates with git show <commit-id> and restore one with git stash apply <commit-id>. This only works until Git cleans up unreachable objects, so act quickly.
"I have too many stashes and don't know which is which"
Always use git stash push -m "message", and review the list with git stash list. Clean out old stashes regularly with git stash drop.
"Stash says 'No local changes to save'"
You have nothing modified in tracked files. Check git status to see whether your changes are in untracked files, in which case add -u.
Best Practices
Always add a message. Future you will thank present you.
Keep stashes short-lived. A stash is a temporary shelf, not long-term storage. For anything that will sit for days, use a branch or a commit.
Prefer
applywhen you are unsure. It keeps a safety copy until you drop it.Check
git statusbefore and after. It confirms what was stashed and what was restored.Remember stash is local. If your laptop dies, so do your stashes. Push important work to a branch.
Conclusion
Now you clearly understand:
What is git stash
What is stash in git
What does git stash do
How to use the git stash command
A practical git stash example
Git stash is a powerful feature that helps you manage unfinished work without committing it. By using stash correctly, you can work faster, switch contexts easily, and keep your repository clean.
If you're learning Git, mastering git stash is an essential skill. Try it on a practice repository: make a change, stash it, switch branches, and bring it back. A few minutes of hands-on practice will make it second nature.
Was this article helpful?



Comments
Leave a comment
Loading comments...