By far the most common "difficult to undo" error I come across amongst those new to git are abuses of git stash
, because git stash pop
isn't always reversible with git stash
. Consider:
git init /tmp/trash && cd /tmp/trash # make a new repo
echo A > A # create a file
git add A
git commit -m "Initial commit" # add and commit it
echo B > A # ... now change it
git stash # ... and stash it
echo C > A # and change it and commit it again
git add A
git commit -m "Another commit"
git stash pop # now pop the stash
The pop
will attempt to automerge A
and will throw a conflict, but you can't just back out of the pop by hitting git stash
again. This is a fairly trivial example, but it's easy to get into a nasty spot if you're stashing large sets of changes often, and switch divergent branches frequently, popping along the way. I believe git stash drop
is also permanent, i.e. there's no safety net if you drop the wrong stash.
In general, use stash
for what it was designed to do: Stash a dirty working index to hop over to another branch, fix a bug, commit, push, and hop back to your original state. If you try to use it to manage a lot of changes across many branches, it will inevitably bite you without a great deal of care.