Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 views
in Technique[技术] by (71.8m points)

git - Can I fetch a stash from a remote repo into a local branch?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...