Saving Changes with Git Stash

In the Basics of Git course, I was in the middle of making some changes to the homepage of our sample site when a another change request came in. I needed to quickly save--or stash away--my changes and then apply them back to the repository later, after my other work was complete.

In the Up and Run­ningn with Git course, I was in the mid­dle of mak­ing some changes to the home­page of our sam­ple site when a anoth­er change request came in. I need­ed to quick­ly save — or stash away — my changes and then apply them back to the repos­i­to­ry lat­er, after my oth­er work was complete.

The eas­i­est way to do this is with git-stash, a use­ful git com­mand that allows you to store the mod­i­fi­ca­tions you’ve made in our work­ing direc­to­ry and go back to a clean work­ing direc­to­ry.

From the Git Man­u­al (run git stash --help to see it on your own):

Use git stash when you want to record the cur­rent state of the work­ing direc­to­ry and the index, but want to go back to a clean work­ing direc­to­ry. The com­mand saves your local mod­i­fi­ca­tions away and reverts the work­ing direc­to­ry to match the HEAD commit.

Git stash isn’t a replace­ment for smart use of branch­es (we don’t want to start a new fea­ture in a main branch and then stash it away until you’re ready to apply it) but can be a life saver if we find our­selves need­ing to quick­ly shift­ing gears to anoth­er task, bug report, or request.

Fire up a cur­rent project to try out git stash. 

First, let’s check if we have any exist­ing stashes. 

git stash list

The list option shows all of the exist­ing stash­es, if there are any. If you do have some they’ll be list­ed like this:


stash@{0}: On develop: testing out git stash
stash@{1}: WIP on master: 4fd1101 fixing layout on homepage product listing
stash@{2}: On develop: product bundle download template

Each stash is list­ed in a sep­a­rate line, start­ing with the stash name that con­sists of the word stash” and an index (start­ing with zero) in curly braces. The lat­est stash is the one at the top, with the index of zero (con­fus­ing, right?).

Let’s add a stash. To do that we use the stash savecom­mand.

stash save "updated the offline file"

The last two parts of that stash com­mand are option­al. We can just do:

stash

instead and it will still cre­ate a stash with any changes in the work­ing direc­to­ry but with­out are cus­tomized mes­sage. The save option is auto­mat­i­cal­ly assumed if it isn’t included.

Here’s the list of stash­es now:


stash@{0}: On develop: updated the offline file
stash@{1}: On develop: testing out git stash
stash@{2}: WIP on master: 4fd1101 fixing layout on homepage product listing
stash@{3}: On develop: product bundle download template

To apply a stash to the work­ing direc­to­ry, we have a cou­ple of options. The first is:

git stash pop

which will remove the most recent stash from the list and apply it to the cur­rent work­ing direc­to­ry. It’s just revers­ing what you did when sav­ing the stash (but keep­ing any sub­se­quent repos­i­to­ry changes intact).

You can also be spe­cif­ic and pop any stash in the list:

git stash pop stash@{1}

We should see some­thing like this at the end of the output:

Dropped stash@{0} (38f88c1479dc8a3c63f794feed7cd276ae3c6c7e)

The oth­er option when apply­ing a stash is to use:

git stash apply

Just like with pop, we can choose to not spec­i­fy a stash and it will use the lat­est. Or, we can spec­i­fy one, like this:

git stash apply stash@{1}

With apply, we’ll get sim­i­lar out­put when apply­ing the stash, but with­out the mes­sage that the stash was dropped. Why? Because git stash apply applies the stash we spec­i­fied but it doesn’t drop it from the list of stashes. 

If we run:

git stash list

We get the full list of stash­es, includ­ing the one we just applied.


stash@{0}: On develop: updated the offline file
stash@{1}: On develop: testing out git stash
stash@{2}: WIP on master: 4fd1101 fixing layout on homepage product listing
stash@{3}: On develop: product bundle download template

These are the basic com­mands when using stash­es day-to-day. There’s more to it, and I encour­age you to run:

git stash --help

to see all of the commands.