Skip to main content

Git for Authors

Section 8.3 The Stash

The stash is a stack, a last-in, first-out arrangment, like a stack of plates in a warmer in a cafeteria. At any time you can save your changes in a dirty directory with the command git stash save and they are removed from the working directory, thus making the directory clean again, and are saved as a changese in the stash. This great if you just want to wander off some place else in your repository and you will be right back to your work-in-progress. So we talk of “stashing your changes” as a temporary measure.
When you are a beginner, the stash is very alluring. Resist the siren call. It is not a cure-all and there is usually a better way to juggle two things at once. Remember the three branches of Checkpoint 2.2.1? Here are some notes (which will not explain everything carefully). Since the stash manipulates the working directory all at once, you have less control than you do with commits, even if the stash seems less permanent.
  • git stash save -- "Reworking turtle chapter" will include a message with the entry you place on the stack. Without it, you get a cryptic automatic message that includes the initialism WIP (work-in-progress). If you will be away more than ten minutes, a message is a good idea.
    This is like putting a plate into the warmer, perhaps with a special note scribbled onto it.
  • git stash show will show you the diff of the changeset on top.
    This is like looking real closely at the top plate.
  • git stash list will show you all the changesets currently in the stash, so it helps to provide good messages if you have many.
    This like like reading the notes on all the plates in the warmer.
  • git stash pop will put your changes back into the working directory and remove the entry from the stack. It is up to you to make sure you are on the branch you want to be, and that your working directory is in the right state to accept these changes (or you may end up starting a merge you may not want).
    This is like taking a plate out of the warmer.
  • git stash apply will take the changes on the top of the stack and put them back into the working dirtectory, but it will leave the original entry on top of the stash. RAB once ended up with duplicate copies of a set of changes in his working directory. He thinks he did an apply and subsequently did a pop. Maybe.
    This is like magically duplicating the plate on top, and removing it, leaving the original still in the warmer.
  • git stash drop will remove the changes on top of the stack and throw them away. This can actually be very useful. You may add and commit a variety of changes to your branch, but still have some paragraphs you do not really like, and decided not to use, still polluting your working directory. Simple. Move the changes to the stash and immediately delete them, no message needed. Careful, think twice, you really are deleting changes, though they may be recoverable with advanced techniques.
    This is exactly like taking the top plate out and dropping it on the floor so it breaks into many unusable pieces.
  • There are many more actions you can take with the stash, but the above should be sufficient for intermediate git use. Consult the usual sources for more advanced use. Note that prior to the time around git version 1.6.0 changesets in the stash expired, but it appers that behavior has changed. So don’t panic if you see older posts that speak of expiration.