52

Is there a way to share a stash in git?

I work on a number of machines, and often want to be able to move my current working state from one to another.

I'm looking for a way that I can push/pull a stash from one clone to another, and have it appear either as the stash for the other clone, or as an apparent remote branch. I'm not expecting that the former will necessarily work if the remote already has stash of its own though.

Given that stash is, in effect, already a branch with commits on it (apparently), I'm not looking for solutions along the lines of "commit each stash to a branch and then share those" - I've already got many, many, branches. I'm therefore looking for the refspec or similar that I can use to control the push/pulling.

PeterJCLaw
  • 2,654

3 Answers3

32

The stash is just stash or refs/stash, which you can push to a remote branch:

git push origin stash@{2}:refs/heads/otherstash

Git will refuse pushing directly to refs/stash, however. Also, there doesn't seem to be a way to push entire reflogs, where previous stashes are stored.

u1686_grawity
  • 452,512
  • 9
    git push origin $(for sha in $(git rev-list -g stash); do echo $sha:refs/heads/stash_$sha; done) should do nicely for all stashes; See also http://stackoverflow.com/a/5248758/85371 – sehe Feb 25 '16 at 15:00
23

It's not as well integrated as a git push but to avoid creating branches, I use git stash show -p > change.patch (after stashing the changes) or even git diff --cached > change.patch to create a patch that I git apply change.patch on the next machine I work from.

With this solution at least, if many files are altered in your current working state, everything is contained in a single data unit.

Neo
  • 331
  • 1
    Yes.. but how ? git stash -p only asks me interactively about every hunk - then spits long line that I don't understand. Where is that "patch" I can take with me to another machine? and what is the git diff--cached? and how do you use git apply? Can you elaborate just a little further please? – Motti Shneor Jan 31 '18 at 11:36
  • I detailed and corrected my answer – Neo Feb 01 '18 at 13:13
  • @Neo thanks for sharing this solution. I am trying to integrate this into my workflow, but am having trouble incorporating new, untracked files using this approach. Do you know of any way of getting new files into the change.patch file? This command stashes new, untracked files git stash --all or git stash --include-untracked, but I cannot get the new, untracked files to show up in the patch file when issuing git stash show -p > change.patch. Any ideas? Thanks again for providing this path – Zhao Li Apr 12 '21 at 09:11
  • nevermind, I finally remembered how to properly use git diff --cached and that works perfectly with new, untracked files. Thanks again for this neat trick. :) – Zhao Li Apr 12 '21 at 09:18
  • in case it helps others:
    1. git add . 2. git diff --staged > ~/some_synced_folder/change.patch 3. (on another sync'd computer) git apply ~/some_synced_folder/change.patch
    – Zhao Li Apr 12 '21 at 09:31
6

You can make patch and send it to someone. the thing you they have to do is to apply it.

git diff >> file.diff