获取远程遥控器和远程远程分支

时间:2011-02-18 12:58:23

标签: git branch

我想获取远程存储库的远程分支。

所以,例如,在这种情况下 -

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

- 我想通过gammaslave的{​​{1}}分支机构提取到alpha存储库。这可能会将beta添加为gamma的远程,并使用alpha作为新分支的refspec。我不一定要创建本地跟踪分支。与示例不同,我也不一定具有gamma/slavebeta的文件系统访问权限。

这种事情可以用gamma来完成,但是有没有办法在已经存在的仓库中做到这一点?

1 个答案:

答案 0 :(得分:0)

看起来设置一个非默认的refspec会让我在那里。

例如,请考虑此设置

  • 初始化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
    
  • 初始化beta并将gamma添加为远程:

    $ (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
    
  • 克隆beta以制作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
    

现在变得激烈:

# 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

所以做一个`$ git fetch'+ refs / :refs / '会拉入分支本身,但是不会更新构成它所属的远程的配置项。

有趣的是,为无遥远的远程分支设置跟踪分支将使其开始跟踪自己的回购:

$ 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

我认为这不起作用。

$ 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