Git Tips #1 - Save a stash with a custom message

This is the first, in what I expect to be a long running series of Git tips for both new and experienced Git users. Some are commands I use, but not often enough to remember the syntax. Others are config changes I like to make to my environment.

The List

The full list of my Git tips is here.

What is a Stash?

For those unfamiliar with a stash in Git, it's a command to save (stash) your pending changes without committing them.

I use it when I'm working on a feature, but get interrupted. This is my workflow:

  1. Stash my pending changes.
  2. Switch to another branch to fix whatever it is.
  3. Switch back to the branch I started on.
  4. Un-stash (pop) my stash.
  5. Continue working on interesting new feature.

It's similar to a shelveset in Team Foundation Version Control, except that it's stored locally in your Git repository and not synced up to the server.

The Command

To create a stash with a custom message, the command is simply:

git stash save "My custom message"

And to un-stash the changes later and delete the stash:

git stash pop

Or if you want to keep the stash:

git stash apply

References

  1. https://git-scm.com/docs/git-stash
  2. http://stackoverflow.com/questions/11269256/how-to-name-a-stash-in-git