开发者

Fetching remote remotes and remote remote branches

I'd like to fetch the remote branches of a remote repository.

So, for example, in this situation —

$ cd alpha
$ git remote
beta
$ git branch -a
master
remotes/alpha/master
$ cd ../beta
$ git remote
gamma
$ git branch -a
master
remotes/gamma/slave

— I'd like to fetch gamma's slave branch into the alpha repository by going through beta. This would presumably add gamma as a remote of alpha and use gamma/slave for the new branch's refspec. I don't necessarily want to create a local tracking branch. I also don't necessarily have filesystem access to beta or g开发者_StackOverflow社区amma, unlike in the example.

This sort of thing can be done with $ git clone --mirror, but is there a way to do it in an already-existing repo?


It looks like setting a non-default refspec will get me partway there.

For example, consider this setup:

  • Initialize gamma

    $ (mkdir gamma; cd gamma; git init; touch README; git add README;
       git commit -m 'Initialized slave.'; git branch -m slave; echo)
    Initialized empty Git repository in /tmp/test-git/gamma/.git/
    [master (root-commit) 0cebd50] Initialized slave.
     0 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 README
    
  • Initialize beta and add gamma as a remote:

    $ (mkdir beta; cd beta; git init; touch README; git add README;
       git commit -m 'Initialized master.';
       git remote add gamma ../gamma; git fetch gamma
       echo; echo "In repo $(basename $PWD):"; git branch -a; echo)
    Initialized empty Git repository in /tmp/test-git/beta/.git/
    [master (root-commit) f6512e1] Initialized master.
     0 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 README
    warning: no common commits
    remote: Counting objects: 3, done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (3/3), done.
    From ../gamma
     * [new branch]      slave      -> gamma/slave
    
    In repo beta:
    * master
      remotes/gamma/slave
    
  • clone beta to make alpha:

    $ git clone beta alpha
    Initialized empty Git repository in /tmp/test-git/alpha/.git/
    $ cd alpha; git branch -a
    * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/master
    

Now get fierce:

# Avoid errors from pulling into active branch.
$ git checkout origin/master  | head -3  # intentionally acquire severed HEAD
Note: checking out 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
$ git fetch origin '+refs/*:refs/*'      # pour steroids into open esophagus
From /tmp/test-git/beta
 * [new branch]      gamma/slave -> gamma/slave
$ git branch -a
* (no branch)
  master
  remotes/gamma/slave
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
$ git remote
origin

So doing a `$ git fetch '+refs/:refs/' will pull in the branch itself, but will not update the configuration items that constitute the remote that it belongs to.

Interestingly, setting a tracking branch for the remoteless remote branch will cause it to start tracking its own repo:

$ git branch --track slave gamma/slave 
Branch slave set up to track local ref refs/remotes/gamma/slave.
$ git config -l | grep '^remote\|^branch'
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
remote.origin.url=/tmp/test-git/beta
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.slave.remote=.
branch.slave.merge=refs/remotes/gamma/slave

I think this doesn't really work.

$ git checkout slave
Switched to branch 'slave'
$ git fetch
From .
 * remote branch     gamma/slave -> FETCH_HEAD
$ git fetch
From .
 * remote branch     gamma/slave -> FETCH_HEAD
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜