A commit permanently saves changes in a repository. But what if you only want to save your changes temporarily? This is where stashes come in handy.
Saving Changes Temporarily
A commit wraps up changes and saves them permanently in the repository. However, in your day-to-day work, there are a lot of situations where you only want to save your local changes temporarily. For example, imagine you're in the middle of some changes for feature X when an important bug report comes in. Your local changes don't belong to the bugfix you're going to make. You have to get rid of them (temporarily, without losing them!) and continue working on them later.
Situations like this one happen all the time: you have some local changes in your working copy that you can't commit right now - and you want or need to start working on something else. To get these changes out of your way and have a "clean" working copy, Git's "Stash" feature comes in handy.
Think of the Stash as a clipboard on steroids: it takes all the changes in your working copy and saves them for you on a new clipboard. You're left with a clean working copy, i.e. you have no more local changes.
Later, at any time, you can restore the changes from that clipboard in your working copy - and continue working where you left off.
You can create as many Stashes as you want - you're not limited to storing only one set of changes. Also, a Stash is not bound to the branch where you created it: when you restore it, the changes will be applied to your current HEAD branch, whichever this may be.
Let's stash away our local changes so we have a clean working copy before starting to work on our new feature: click the "Save Stash" button in Tower and (optionally) enter a short description so you'll later understand these changes.
The local changes in "icon.png" are now safely stored on a clipboard, ready to be restored any time we want to continue working on them.
You can easily get an overview of your current Stashes by clicking the "Stashes" item in Tower's sidebar:
When you're ready to restore a saved Stash, you can click the "Apply Stash" button in the toolbar and select the appropriate item from the list.
When to Stash
Stashing helps you get a clean working copy. While this can be helpful in many situations, it's strongly recommended...
...before checking out a different branch.
...before pulling remote changes.
...before merging or rebasing a branch.
Finally, it's time to get our hands dirty with our new feature!
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.
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.