Reputation: 5241
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.
Upvotes: 452
Views: 194049
Reputation: 942
This approach ensures a smooth transfer of work between computers while maintaining the version history.
The trick is that we commit the changes, push to remote, pull on destincation, create a patch from commit, and then revert the commit. from there we apply the patch that contains our changes.
Remember to replace your_branch_name with actual BRANCH name.
Step 1: Commit Changes on Source Computer:
git add .
git commit -m "WIP"
Step 2: Push Changes to Remote:
git push origin your_branch_name
Step 3: Pull Changes on Destination Computer:
git pull origin your_branch_name
Step 4: Create a Patch from last commit that just pulled:
git checkout your_branch_name
git format-patch HEAD^ -o .
git add .
git stash save "Patch"
Step 5: Revert the WIP Commit:
git reset HEAD^
git add .
git commit -m "revert WIP"
Step 6: Pop Stash and Apply Patch:
git stash pop
git apply --ignore-space-change --ignore-whitespace 0001-wip.patch
This sequence of commands allows you to export changes from one computer to another using Git's version control features. It involves committing, pushing, pulling, generating a patch, stashing, reverting, and applying the changes.
Upvotes: 1
Reputation: 1
Based on @Chris Maes answer
Running this NodeJS script on repo root may create a patch file for every existing stash:
const fs = require('fs');
const cp = require('child_process');
try{
fs.mkdirSync('./stashes')
}catch(err){console.log("Folder stashes already exists")}
var stashes = cp.execSync("git stash list").toString().split('\n');
for(var stash of stashes){
var stashId = stash.split(":")[0];
var patchFilename = stash.replace(/[/\\?%*:,|" <>]/g, '_');
cp.execSync(`git stash show ${stashId} -p > ./stashes/${patchFilename}.patch`);
}
Upvotes: 0
Reputation: 151
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}
Upvotes: 3
Reputation: 1589
Alternatively, you can copy the entire local stashes(+ other local branches, local tags, etc) to another computer as follows:
git pull
on both your old and new git directory to ensure that both have the latest changes (Or make sure that both repos have the same HEAD
using git reset --hard commit-hash
).Upvotes: 29
Reputation: 83
git --no-pager stash show -p > patch
Copies the stashed changes to patch file
git apply path/to/the/patch/file
Applies stash from the patch file
Following shell script allows users to copy all stashes from one folder to another folder. https://gist.github.com/senthilmurukang/29b55a0c0e8694c406991799153f3c43
Upvotes: 8
Reputation: 2510
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:
stash_
+ the number(s) in the logical stash ref)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:
for ref in $refs; do git fetch $oldpath $ref; git stash store -m "$(git show -s --pretty=%s $ref)" $ref; done
Upvotes: 13
Reputation: 2830
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
Upvotes: 166
Reputation: 624
Apply your stash to it and make a commit
Click on your commit and make a patch from it, take the patch file with you.
Go to a different repository, select the same parent branch which you just used in 1)
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
Make a new Stash for the current repo
Upvotes: 10
Reputation: 211
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
Upvotes: 0
Reputation: 61
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.
Upvotes: 6
Reputation: 12062
Another option is to rsync
the .git
folder from one computer to another computer. rsync
processes only file changes (faster than a copy).
One downside to this approach is the configs would also be overwritten, which may not be desired if you run different .git configs between the two machines. But you could overcome this by excluding files with the --exclude
option in rsync
.
Overall I think a native Git solution is cleaner, but this rsync
hack could be nice for someone in a hurry who might be more familiar with rsync than git.
Upvotes: 8
Reputation: 37782
alternatively you can create a branch from your stash (on computer 1), using
git stash branch stashed_changes_branch
commit your changes:
git commit -a
then add it as a remote on computer 2:
git remote add pc1 user@computer1:/path/to/repo
now you can retrieve the remote information using
git fetch pc1
now you can import the commit in the way you want; using git cherry-pick, git rebase or whatever you like... If you want it to look like you just did git stash apply; you can use git cherry-pick --no-commit.
If you have no direct connection between computer1 and computer2; you can use a remote (like github or something similar):
git push origin stashed_changes_branch
and on computer2:
git fetch
Upvotes: 23
Reputation: 388023
You can apply a patch file (without committing the changes yet) by simply running
git apply patchfile
Then you can simply create a new stash from the current working directory:
git stash
Upvotes: 375