FizzBuzz Journal

Not really a blog, just some stuff that a future me might need to remember one day.

  1. Github
  2. Stackoverflow
  3. Twitter

Git stash

22 Aug 2014

git stash is a quick and dirty tool for putting a set of changes to one side, so that they can be later retrieved without having to make a commit.

The simple usage is git stash to stash uncommitted/unstaged changes, get on with whatever else you needed to do, then some time later git stash pop to reapply those changes. This is handy but only gets you so far.

What if you have multiple stashes? How do I see those changes and how can I retrieve them?

Looking inside a stash

To compare stashed changes with the current head:

$ git stash show -p stash@{0}

stash@{0} is just a reference to the last stash that you saved. stash@{1} is the previous one and so on.

If you just want to compare against the commit the stash was based on:

$ git diff stash@{0}^!

Listing stashes

You can see the list of stashes with:

$ git stash list
stash@{0}: WIP on feature-branch: 1a345b7 Commit message
stash@{1}: WIP on master: 76b43a1 Commit message

Normally the stash doesn’t have a message to tell what it contains so git just outputs what it does know about the stash: the branch name and commit SHA and message on which it was based.

Give stashes a description

If you want to give a stash a meaningful label:

$ git stash save 'some change I want to keep for later'

git stash is short for git stash save. These descriptions will show up in the output from git stash list.

Popping an older stash

So if you have stashed something useful a while back and want to quickly reapply the change, first use git stash list to get the reference to the stash you want and then (say its number 6):

$ git stash pop stash@{6}

Partial stashes

So you have made a bunch of changes and you want to split them up by stashing some of them and leaving the remaining changes in place. You can do this a couple of ways.

$ git stash -p

-p is short for --patch and is a kind of interactive stash that will prompt you to add hunks of changes one at a time.

You can also stage files with git add ... and then stash unstaged changes with:

$ git stash --keep-index

Converting to a branch

It probably doesn’t make a lot of sense to try to do any more with stashes, you would probably be better off creating a proper branch:

$ git stash branch new-branch-from-stash