Saving Changes Temporarily
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 featured image

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.

Concept

The Stash

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 these local changes so we have a clean working copy before starting to work on our new feature:

$ 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
$ git status
# On branch master
nothing to commit (working directory clean)

The local changes in "imprint.html" 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:

$ git stash list
stash@{0}: WIP on master: 2d6e283 Implement the new login box

The newest Stash will always be at the top of the list, named "stash@{0}". Older Stashes have higher numbers.

When you're ready to restore a saved Stash, you have two options:

  • (a) Calling "git stash pop" will apply the newest Stash and clear it from your Stash clipboard.
  • (b) Calling "git stash apply <stashname>" will also apply the specified Stash, but it will remain saved. You can delete it later via "git stash drop <stashname>".

You can choose to not specify the Stash when using any of these commands. Then, Git will simply take the newest Stash (always "stash@{0}").

Concept

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!

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.