A stash is a special merge commit of the work tree between the base commit and the index. One way could be to save each as separate patches, checkout the stash first parent, restore the index and work tree from the two patches and finally restore the stash (it seems one answer goes this way).
This is needed to fully recreate all information from the stash, and if you don't care about that you should at the very least checkout the stash's first parent before restoring to avoid conflicts and keep track of where the stash was created.
This is what I did to fully restore all stashes from one repo to another. If you can't have them on the same computer, you can save the stash tags in a bundle after creating them and copy the refs list and bundle to the target computer.
From the root of the original repo:
- Get the list of stash refs
- Tag your stash refs so you can retrieves them with git fetch (the tag names doesn't mater, change it if there is a conflict. I used
stash_
+ the number(s) in the logical stash ref)
- Convert the logical refs to sha1 hashes in reverse order - we'll use them later
- Save that repo path - also for later
refs=$(git stash list|cut -d: -f1)
for ref in $refs; do git tag stash_${ref//[^0-9]} $ref; done
refs=$(git rev-parse $refs|tac)
oldpath=$PWD
NB: This requires bash or compatible shell (ksh, zsh should do...) You could also increment a variable, ex stash_$((i++))
if your shell doesn't support ${param//pattern}
Now in the new repo, for each ref:
- Fetch the ref from the old repo (we don't even need to use the tag names, because we have tagged them we can retrieve them with git fetch)
- Re-import the stash from the ref, using that ref's subject as the stash message.
for ref in $refs; do git fetch $oldpath $ref; git stash store -m "$(git show -s --pretty=%s $ref)" $ref; done
git stash save
is now deprecated in favour ofgit stash push
git stash show
ignores untracked files, even if they are stashed.git 2.32.0
was released with[-u|--include-untracked|--only-untracked]
forgit stash show
. My substitute commands for those in the question are nowgit stash push --include-untracked
andgit stash show --include-untracked --patch > patch
.