Alternatives to git stash

25 Mar 2014

Amber Yust has written a very handy article about why you may want to avoid using git stash. I just want to highlight a technique she includes at the end of her article as an alternative.

You really should read her article, but if you want the short version, here’s the little script she includes to give you similar functionality, but using branches instead of the stash:

[alias]
    save = !sh -c 'export PREV=$(git symbolic-ref HEAD|cut -d/ -f3-) && git checkout -b "$1" && git commit -am "$1" && git checkout "$PREV"' -

As she says:

With this git alias, you can do git save foobar and it will:

  • Create a branch named “foobar”
  • Commit any changes on that branch
  • Swap you back to the branch you started on

All with a single command, just like git stash does, but with none of the drawbacks of the stash system.