Mastodon Skip to main content
Log

Stash Hard

If you're familiar with git, you're probably familiar with git stash, which is a very useful way to tuck something away for a moment—i.e. if you need to switch to another branch. I've used it a lot, but I've found that I have an unfortunate tendancy to kind of...forget what I put there, or when (yes, I know that you can use -m to specify what a message for a stash entry, and that by default it uses the last commit for the message). I'll often switch back to my branch, do a bit of work...and then remember that my stash was there, pop it, get a bunch of conflicts, and generally have a bad time.

Are there stash tools and commands that could help me deal with this? Almost certainly, and someday I might even get around to learning them, but—especially for the specific use case of "I need to hop out of this branch for a moment to work on or look at something else"—I've found myself using the following "hard stash" workflow a lot:

  1. Stage all unstaged files (i.e. get add --all if you like living on the edge).
  2. Add a "WIP" commit, i.e. git commit -m "WIP".
  3. Leave the branch and do whatever you need to do.
  4. Check out the branch you were working on, and run git reset --soft HEAD~1. This restages your last commit (the WIP one) and removes the commit from your history.

So:

$ git add --all
$ git commit -m "WIP"
$ git checkout some-other-branch
$ # Do some other stuff
$ git checkout original-branch
$ git reset --soft HEAD~1
$ # Back to work...