Export a stash to another computer

Viewed 128027

I need a way to export a stashed change to another computer.

On computer 1 I did

$ git stash save feature

I'm trying to get the stash patch to a file and then import it to another computer

$ git stash show -p > patch

This command gives me a file that I can move to another computer where this repo is cloned, but the question is how to import it as a stash again.

11 Answers

You can create stash as patch file from one machine,then can share that patch file to another machines.

Creating the stash as a patch

$ git stash show "stash@{0}" -p > changes.patch

The “stash@{0}” is the ref of the stash.It will create patch file with latest stash. If you want different one use command $ git stash list to see your list of stashes and select which one you want to patch.

Applying the patch

Now transfer that stash to another machine and paste it into the root folder of your project. Then run this command

$ git apply changes.patch

If there is mistake and you want to reverse the change

$ git apply changes.patch --reverse

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:

  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

How to do export Stash in SourceTree:

  1. Make a new branch "StashTransfer" from a branch where you are going to use your Stash
  2. Apply your stash to it and make a commit

  3. Click on your commit and make a patch from it, take the patch file with you.

  4. Go to a different repository, select the same parent branch which you just used in 1)

  5. Actions / Apply Patch, select Mode: Modify working copy files, push Apply Patch now you have uncommitted modifications from the patch in your current working environment

  6. Make a new Stash for the current repo

The startup command from the original post:

git stash show -p stash@{x} > patch_file

didn't work for me (for some reason it created unusable patch files). Instead I had to:

git stash apply stash@{x}
git commit

for each stash I wanted to transfer. Then, I placed the 'parent' repo within file:/// reach of the 'child' repo, and did the following, for each stash commit:

git fetch file:///path_to_parent_git && git cherry-pick commit_sha
git reset --soft HEAD^
git stash save my_new_stash_on_child

This is more complex but did the trick for me.

Pay attention also to the local files stashed with option --binary, and several stashed when export and import. After searching a while, I did the following.

#check all the stashed

git stash list

#export stash by number, --option binary is important to export binary files

git stash show stash@{0} -p --binary > patch0

#import stash, go to new repository

git apply /old_repository/patch0

#Then re-stash the local changes

#repeat the process for all the stashes stash@{1}, ... ,stash@{n}

If you want to move your changes from one machine to another you could always commit your changes on your machine and then do a soft reset on their machine.

Office

git commit -m "-stash-"

Kitchen

git reset --soft HEAD~1

Related