Skip to content

Instantly share code, notes, and snippets.

@alexeds
Created September 5, 2012 18:00
Show Gist options
  • Save alexeds/3641372 to your computer and use it in GitHub Desktop.
Save alexeds/3641372 to your computer and use it in GitHub Desktop.
Move your stashes from one repo to another

Move your stashes from one repo to another


This was useful for me when we created a new branch for a new major release, but were still working on our current version as well. I cloned our repo again and kept the new project on our new branch, but also wanted to get my stashes there.

Download your stashes

git stash show -p > patch

You'll have to specify your stash and name your file whatevery you want. Do this for as all your stashes, and you'll have patch files in your pwd.

Apply your stashes

cd /new/project/dir
git apply /old/project/dir/patchfile
git stash
@andy-shev
Copy link

andy-shev commented Jan 4, 2024

@dermoth, in zsh I needed to wrap the creation of the two refs arrays in (…), otherwise I just ended up with a single string element:

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

Other than that, your solution works a treat! Thank you! 🙏 🙇

Much easier is to have

git stash list | cut -d: -f1 | while read ref; do ...

It will work in any shell.

@andredsp
Copy link

@coroa For macOS, I had to replace rev with tail -r. Otherwise, this worked great!

To transfer all stashes from an old repository to a new one, you can:

  1. Grab hashes of all references in the stash (in reverse order):
cd /path/to/old/repo
hashes=($(git reflog --format=%H refs/stash | rev))

macOS version:

hashes=($(git reflog --format=%H refs/stash | tail -r))
  1. Switch to new repository, fetch them and put them back into the stash reflog
cd /path/to/new/repo
git fetch /path/to/old/repo $hashes
for hash in $hashes; do git stash store -m "$(git show -s --format=%s $hash)" $hash; done

@coroa
Copy link

coroa commented Feb 23, 2024

@andredsp Glad it was of help! Thanks for the hint! Fixing it above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment