git stash - How to Save Your Changes Temporarily
There are lots of situations where a clean working copy is recommended or even required: when merging branches, when pulling from a remote, or simply when checking out a different branch.
The "git stash" command can help you to (temporarily but safely) store your uncommitted local changes - and leave you with a clean working copy.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
When to Use git stash
There are several common scenarios where stashing your changes makes sense:
| Scenario | Why stash helps |
|---|---|
| Switching branches mid-work | You need a clean working copy to check out another branch |
| Pulling remote changes | Your local edits conflict with incoming changes from git pull |
| Testing a clean state | You want to verify something works without your uncommitted changes |
| Quick experiments | You want to try something without committing unfinished work |
git stash: a Clipboard for Your Changes
Let's say you currently have a couple of local modifications:
$ git status
modified: index.php
modified: css/styles.css
If you have to switch context — e.g. because you need to work on an urgent bug — you need to get these changes out of the way. You shouldn't just commit them, of course, because it's unfinished work.
This is where "git stash" comes in handy:
$ git stash
Saved working directory and index state WIP on master:
2dfe283 Implement the new login box
HEAD is now at 2dfe283 Implement the new login box
Your working copy is now clean: all uncommitted local changes have been saved on this kind of "clipboard" that Git's Stash represents. You're ready to start your new task (for example by pulling changes from remote or simply switching branches).
You can also save a stash with a descriptive message using git stash push -m:
$ git stash push -m "login feature WIP"
This makes it much easier to identify your stashes later, especially if you accumulate several of them.
Continuing Where You Left Off with git stash pop
As already mentioned, Git's Stash is meant as a temporary storage. When you're ready to continue where you left off, you can restore the saved state easily:
$ git stash pop
The "pop" flag will reapply the last saved state and, at the same time, delete its representation on the Stash (in other words: it does the clean-up for you).
In case you want to apply a specific Stash item (not the most recent one), you can provide the index name of that item in the "pop" option:
$ git stash pop stash@{2}
Note: There is no git unstash command. To restore stashed changes, use git stash pop or git stash apply (see below).
Stash Apply vs Pop: Keeping Your Stash Around
If you want to restore your changes but keep them on the Stash (e.g. to apply the same changes to multiple branches), use apply instead of pop:
$ git stash apply
| Command | Restores changes | Removes from Stash |
|---|---|---|
git stash pop |
Yes | Yes |
git stash apply |
Yes | No |
You can also keep your staged files in the working directory while stashing everything else with --keep-index:
$ git stash push --keep-index
Does git stash Include Untracked Files?
By default, git stash only saves changes to tracked files. New files that haven't been added to Git yet (untracked files) are left behind.
To include untracked files, use the -u flag (short for --include-untracked):
$ git stash -u
Or in its long form:
$ git stash push --include-untracked
You can combine it with a message:
$ git stash push -u -m "WIP including new files"
If you also need to stash ignored files (e.g. build artifacts), use -a (short for --all):
$ git stash --all
Autostash limitation: The --autostash option used with git pull or git rebase runs a plain git stash internally — it does NOT include untracked files. If you have untracked files that conflict, stash them manually first:
$ git stash -u
$ git pull --rebase
$ git stash pop
Stashing Specific Files
You don't have to stash everything at once. To stash only certain files, pass their paths after --:
$ git stash push -m "just the CSS" -- css/styles.css
You can also list multiple files:
$ git stash push -- file1.js file2.js
For even more control, use --patch to interactively select which changes to stash:
$ git stash push --patch
Troubleshooting: "No Local Changes to Save"
If you run git stash and see this message, it means Git didn't detect any changes to stash. This usually happens for one of two reasons:
1. You only have untracked (new) files. A plain git stash ignores them. Use -u instead:
$ git stash -u
2. Your changes are in ignored files. Files listed in .gitignore are also skipped by default. Use -a:
$ git stash --all
Tip
Using the Stash in Tower
In case you are using the Tower Git client, saving to and restoring from the Stash can be performed right from the toolbar. Tower even lets you restore the exact state of your Working Copy when you restore a Stash:
Learn More
- Check out the chapter Saving Changes Temporarily in our free online book
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.