git reset --soft as stash replacement and undo

Written . Tagged Git.

With Git, if you have some work in progress and find you need to switch to another incompatible branch, you can git stash the changes and later restore them with git stash pop.

However, I dislike having to remember I have something stashed. It’s easy to forget. (Idea: make the shell prompt indicate stash.) Instead, when I have work in progress, I just commit it as usual, with something like “WiP” as the message:

git commit -a -m "WiP"

When I get back to a branch, find that the last commit is “WiP” and want to restore my previous state, I do

git reset --soft HEAD^

This will leave the working tree in the WiP state, but roll back the current HEAD one commit, as though you never made that commit.

Just remember not to share the branch with a WiP commit or someone else may build upon it and object when you rewrite history.

More generally, git reset --soft HEAD is great for undoing non-pushed commits. If I realize I committed a file I shouldn’t have, I do a soft reset and then redo the commit properly. It can be easier than amending or interactively rebasing.