I regret having applied a stash (wrong branch). How can I undo this and have my stash back to my stash list in order to apply it later on the right branch?
If you had uncommitted changes in your working space before you did the git stash apply, there is no generic answer.
But if your working space was clean (no uncommitted changes) before you did the git stash, one of these two cases should help you:
If you haven't committed, you should just be able to git stash
again, possibly with a git reset HEAD
first. Be sure when you git stash apply
to reference the right stash in this case -- e.g. git stash apply stash@{1}
. See "New file from a stash or file rename in a stash" below if one of these cases applies to you.
Also, git stash apply
doesn't delete the stash like git stash pop
does. So if you have committed, you could git reset --hard [last_good_commit]
(if you haven't pushed) or git revert [last_good_commit]
(if you have pushed) and just apply the stash again once you're on the right branch.
Note: Running git reset --hard
will delete any uncommitted code.
The first approach (git stash
, possibly after a git reset HEAD
) assumes that the stash makes changes only to existing files. If the stashed change created a new file or renamed an existing file, git reset HEAD
will cause the new file to become an "untracked" file that remains in your workspace. You will need to manually delete this file to completely unstash the changes.
Neither of the approaches above will work if you had uncommitted changes to files in the working space before you applied the stash (Even if you added these files to the index with git add ...
). That case is particularly difficult to work with because the changes from the git apply stash
are made directly to the files in the workspace and the index, so the two sets of changes are mixed together in the same sets of files. There's no generic solution for this case.