Git Stash, Retrieving Items That Are Not the Most Recent

Some Git commands like stash show are used regularly but not enough to keep top of mind. These are some commands I often find myself Googling for. If you are new to Git, checkout this post, Git 101 for the basics.

What is Stash?

Stashing work is a great way to set things aside for a moment. For example, I am working on a feature but a customer support request comes in that I want to see locally. No problem.

git stash

Now I can safely switch to master and see what is going on. When I am ready to jump back to my work:

git stash apply


git stash pop

I typically use the first one since the last not only gives me the last stash item, but also removes it from the stash history.

What if I want an older stash, not the most recent one?

This comes up for me occasionally and is one I find myself Googling for specific options. In this case:

git stash list

This brings up a nice list of your previous stashes if you haven’t been popping them. They will look something like this:

  • stash@{0}: WIP on branch-name: (last commit hash and message here)
  • stash@{1}: WIP on branch-name: (last commit hash and message here)
  • etc.

This is fairly helpful but it doesn’t really show what files I was working on. From here we can show what has been changed within a stash, like this:

git stash show stash@{1}

This will bring up a list of files and the number of edits within each one. Good, good. Now, let’s see the actual changes within the files by adding an option -p. This allows us to see it in patch form.

git stash show -p stash@{1}

This will bring up a list of all changes within that specific stash. This display is limited and you may need to type f when the : displays on this screen to see more. Type h to get a list of all options here and q to leave the patch screen.

It may be a good idea to clean the stash out from time to time, remove all:

git stash clear

Or, if you want to remove specific ones:

git stash drop stash@{1}

Heads up: this renumbers your stashed items.

Stash also has a push option where you can add a message. I just used this to note an attempt at a feature that wasn’t working out. There are possible bits that I may want to reference later though. I can stash this code away like this:

git stash push -m 'attempt at adding user editable table cells'

For full documentation of the stash feature, take a look at the Git Stash Documentation.