Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@gombosg

This comment has been minimized.

Copy link

@gombosg gombosg commented Oct 4, 2017

This works wonderfully and it's very useful! Thanks!

@ambas

This comment has been minimized.

Copy link

@ambas ambas commented Dec 6, 2017

Thanks!

@shazam19

This comment has been minimized.

Copy link

@shazam19 shazam19 commented Jul 9, 2018

Really useful, thanks!

@VaughanJackson

This comment has been minimized.

Copy link

@VaughanJackson VaughanJackson commented Oct 13, 2018

Thank you for this useful tip!

@kha1989led

This comment has been minimized.

Copy link

@kha1989led kha1989led commented Dec 13, 2018

Thanks a lot for this tip. Really helpful!

@Monstarrrr

This comment has been minimized.

Copy link

@Monstarrrr Monstarrrr commented Mar 8, 2019

Got an "error: can't open patch 'my/old/project/dir': Permission denied"

@LukasLSC

This comment has been minimized.

Copy link

@LukasLSC LukasLSC commented Apr 9, 2019

Thank you very much! :)

@Jacob-Stevens-Haas

This comment has been minimized.

Copy link

@Jacob-Stevens-Haas Jacob-Stevens-Haas commented May 24, 2019

🦆 🦆 🐐

Awesome!

@achmiral

This comment has been minimized.

Copy link

@achmiral achmiral commented Jun 10, 2019

Awesome! 🔥 🔥 🔥

@msundari

This comment has been minimized.

Copy link

@msundari msundari commented Jul 16, 2019

Thank you . It worked like a charm.

  1. git stash list ( find out the stash name)
  2. git stash show -p stash@{6} > patch ( put the stash name and save).
@abiemann

This comment has been minimized.

Copy link

@abiemann abiemann commented Aug 8, 2019

I made a stash that included untracked files... this is the result :
error: cannot apply binary patch to 'blah_blah.aar' without full index line

@calerocalero

This comment has been minimized.

Copy link

@calerocalero calerocalero commented Aug 19, 2019

Got errors...
$git apply patch
error: patch failed: .../public_html/WEB-INF/web.xml:220
error: .../public_html/WEB-INF/web.xml: patch does not apply

@Thilakeswar

This comment has been minimized.

Copy link

@Thilakeswar Thilakeswar commented Aug 19, 2019

Is there a way to move all the stashes from one repo to another repo. I have around 50 stashes in my old repo which needs to be moved to my new repo. Any way I can move the all the stashes in one go?

@mjakubowski

This comment has been minimized.

Copy link

@mjakubowski mjakubowski commented Feb 17, 2020

if your patch can't be applied, it might be because of color information being exported as well. in that case generate patch without color:
git stash show --no-color -p > patch

@jilleJr

This comment has been minimized.

Copy link

@jilleJr jilleJr commented Apr 1, 2020

To extend @mjakubowski's answer, if you have a custom pager (such as so-fancy/diff-so-fancy or dandavison/delta) then disabling the pager helps:

git --no-pager stash show -p > patch

@eerikmikael

This comment has been minimized.

Copy link

@eerikmikael eerikmikael commented Apr 7, 2020

Thanks! This just works!

@suryakun

This comment has been minimized.

Copy link

@suryakun suryakun commented Jun 16, 2020

Thank you, it save my time..

@dermoth

This comment has been minimized.

Copy link

@dermoth dermoth commented Jun 18, 2020

While this works, it does not replicate stashes as-is. 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 (I haven't checked this but it could be non-trivial) and finally restore the stash.

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:

  1. Get the list of stash refs
  2. 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)
  3. Convert the logical refs to sha1 hashes in reverse order - we'll use them later
  4. 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:

  1. 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)
  2. 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
@slaingod

This comment has been minimized.

Copy link

@slaingod slaingod commented Jul 4, 2020

for ref in $refs; do git fetch $oldpath $ref; git stash store -m "$(git show -s --pretty=%s $ref)" $ref; done

Thank you so much @dermoth! This worked flawlessly when I switched from having 4 copies of my repo (repo sets actually, using mu-repo) to a single copy with worktrees, I needed to merge my stashes from the various copies. Worked like a charm in Git Bash, with one typo fix....needed to use 'do git tag' instead of 'do tag'.

@dermoth

This comment has been minimized.

Copy link

@dermoth dermoth commented Jul 4, 2020

You're welcome @slaingod and thanks for pointing out the typo... I also posted in on StackOverflow for a similar question.

@13501275443

This comment has been minimized.

Copy link

@13501275443 13501275443 commented Aug 7, 2020

Nice sharing!

@adrianosotos

This comment has been minimized.

Copy link

@adrianosotos adrianosotos commented Aug 26, 2020

Thanks!! Works very well

@dallas

This comment has been minimized.

Copy link

@dallas dallas commented Nov 6, 2020

@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! 🙏 🙇

@mshafeeqkn

This comment has been minimized.

Copy link

@mshafeeqkn mshafeeqkn commented May 17, 2021

I have one other solution.

  1. update both source and destination repository using git pull or git fetch
  2. copy the .git folder from the source repository to the destination repository.

This method will be more simple if you have multiple stashes. But may take more time to copy if your repo is too big.

@spielhoelle

This comment has been minimized.

Copy link

@spielhoelle spielhoelle commented Jun 27, 2021

Super useful 🚀

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