Git Stash is Your Friend

This is a quick how-to guide on git stash and why it’s awesome.

git stash is a super easy way to, well, stash local changes you haven’t committed yet. Stashes are kept safely tucked away, letting you do other things. This is incredibly useful for reasons I’ll get to later.

Using Git Stash

Stashing is easy:

git stash save "I'll come back to this later"

You can have multiple stashes saved at the same time. To see a list of all your stashes, run:

git stash list

And you’ll see something like this:

stash@{0}: On branch1: Adding more ponies to site footer
stash@{1}: On branch1: Tightening up the graphics on level 3
stash@{2}: On branch2: I'll come back to this later

As you can see, each stash gets its own designation, which you can use to restore a particular stash:

git stash apply stash@{2}

If you ever decide you don’t want a stash anymore, you can drop it:

git stash drop stash@{2}

The stash list operates as a stack. The most recent stash is always stash@{0}, second most recent is stash@{1}, and so on. Because of the stack nature, you can also use the shorter commands git stash pop and git stash drop, which will apply and drop the most recent stash, respectively.

One more thing: you can type just git stash and it will use the last commit message as the stash description, which probably doesn’t describe your stash at all. Useful for a quick temporary stash, but confusing otherwise.

Why It’s Awesome

So what can you use git stash for?

  • Obvious case. You’re in the middle of something and an urgent issue pops up. Stash your work in progress and get to work on the urgent issue.
  • You need to move your work in progress to another branch. git stash, git checkout otherbranch, git stash pop.
  • Something just broke on your dev box and you’re not sure if your new code is causing it. git stash, test code.
  • Or sometimes you just have a little code experiment you want to keep local, not quite worth creating a branch for. Keep it stashed.

Hey, you reached the end!

Feel free to check out my other blog posts or subscribe to my RSS feed. You can also click a tag below to see related blog posts.