19

A colleague has a stash in their repository which I can access (via the filesystem), and I'd like to pull that stash into a branch in my repository.

% git ls-remote ~alice/work/repo/ stash
3ccc82fb1ee0e7bde1250c7926d333ce21c109c0        refs/stash

But when I try to fetch that, git tells me "unable to find 3cc82..."

% git fetch ~alice/work/repo stash:new_branch
remote: Total 0 (delta 0), reused 0 (delta 0)
error: unable to find 3ccc82fb1ee0e7bde1250c7926d333ce21c109c0
fatal: object 3ccc82fb1ee0e7bde1250c7926d333ce21c109c0 not found

Is there a way I can fetch the remote stash?

Matt Curtis
  • 23,168
  • 8
  • 60
  • 63

3 Answers3

13

Yes, you can, partially. The stash is just another ref. You can fetch refs which are not heads (branches) by specifying a refspec with the full ref path.

git fetch some-remote +refs/stash:refs/remotes/some-remote/stash
git stash apply some-remote/stash

You can configure this up to fetch the stash when you run an ordinary fetch, too:

git config --add remote.some-remote.fetch +refs/stash:refs/remotes/some-remote/stash
git fetch some-remote
git stash apply some-remote/stash

But this will fail if there is no stash with a "Invalid refspec" as the ref doesn't exist, so you're probably better off doing it on demand. You could set up an alias like:

cat > /usr/local/bin/git-fetch-stash
git fetch --verbose "$1" +refs/stash:refs/remotes/"$1"/stash 
^D
chmod +x /usr/local/bin/git-fetch-stash

git fetch-stash some-remote

The caveat is that you cannot fetch multiple stashes. These are stored as entries in the reflog, and you cannot fetch a remote's reflog.

sj26
  • 6,725
  • 2
  • 27
  • 24
9

Update: A direct answer to the original poster's question is:

git send-pack ./ 3ccc82fb1ee0e7bde1250c7926d333ce21c109c0:refs/heads/tempbranch

'tempbranch' will be at the latest stash (stash@{0}) from the remote. Unfortunately I don't think the reflog is fetched from remote branches, so there is no way to get at the other stashes, unless you have access to the source repo.

Scripting it: I posted a more comprehensive 'scripted' solution over at the mentioned question

Is it possible to push a git stash to a remote repository?

Also, as I discovered in the meantime, git-send-pack can be instrumental if you have access to the source repo:

git send-pack ../myworkingfolder/ stash@{0}:refs/heads/collegue_stash
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    Thanks, I saw that (and upvoted it). However this question is trying to solve a different problem; I could see the remote repo but my colleague had stashed instead of committed, then gone home and told me about the stash, so I just wanted to grab that stash. A couple of longer winded solutions present themselves (e.g. make a patch and apply it locally) but I was looking for a direct way to fetch the stash into my repo. – Matt Curtis Mar 11 '11 at 22:17
  • Edited comment with more insights – sehe Mar 13 '11 at 02:40
1

You can't but this provides you an alternate path. is-it-possible-to-push-a-git-stash-to-a-remote-repository

Community
  • 1
  • 1
naven87
  • 964
  • 1
  • 7
  • 24
  • Hi naven87. I did see that other SO question but it didn't suit what I needed. Thanks for your answer, and I will accept it after a while if no-one else posts a more direct solution. – Matt Curtis Feb 13 '10 at 05:52