Export a stash to another computer

前端 未结 9 892
小蘑菇
小蘑菇 2020-12-02 03:43

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

相关标签:
9条回答
  • 2020-12-02 03:59

    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
    
    0 讨论(0)
  • 2020-12-02 04:02

    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.

    0 讨论(0)
  • 2020-12-02 04:04

    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
    
    0 讨论(0)
  • 2020-12-02 04:13

    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
    
    0 讨论(0)
  • 2020-12-02 04:13

    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

    0 讨论(0)
  • 2020-12-02 04:15

    Alternatively you can export the entire local stashes to another compter as follows

    • git pull on both your old and new git directory to ensure that both having latest changes.
    • copy the .git folder from old git directory to new repository
    0 讨论(0)
提交回复
热议问题