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

前端 未结 3 423
悲哀的现实
悲哀的现实 2020-12-01 18:11

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-rem         


        
3条回答
  •  伪装坚强ぢ
    2020-12-01 18:56

    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.

提交回复
热议问题