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

发布于 2024-10-17 19:03:18 字数 566 浏览 7 评论 0原文

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

因此,例如,在这种情况下 —

$ 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 分支将其提取到 alpha 存储库中代码>测试版。这可能会添加 gamma 作为 alpha 的远程,并使用 gamma/slave 作为新分支的 refspec。我不一定想创建本地跟踪分支。与示例不同,我也不一定具有对 betagamma 的文件系统访问权限。

这种事情可以通过 $ git clone --mirror 来完成,但是有没有办法在已经存在的存储库中做到这一点?

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 gamma, 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦笑流年记忆 2024-10-24 19:03:18

看起来设置一个非默认的 refspec 会让我半途而废。

例如,考虑以下设置

  • 初始化gamma

    $ (mkdir gamma; cd gamma; git init; touch README; git add README;
       git commit -m '初始化从站。'; git 分支 -m 从属;回声)
    在 /tmp/test-git/gamma/.git/ 中初始化空 Git 存储库
    [master (root-commit) 0cebd50] 已初始化从站。
     0 个文件已更改、0 个插入(+)、0 个删除(-)
     创建模式 100644 自述文件
    
  • 初始化 beta 并将 gamma 添加为遥控器:

    $ (mkdir beta; cd beta; git init; touch README; git add README;
       git commit -m '初始化主控。';
       git 远程添加伽玛../伽玛; git 获取伽玛
       回声; echo "在仓库 $(basename $PWD) 中:"; git分支-a;回声)
    在 /tmp/test-git/beta/.git/ 中初始化空 Git 存储库
    [master (root-commit) f6512e1] 已初始化 master。
     0 个文件已更改、0 个插入(+)、0 个删除(-)
     创建模式 100644 自述文件
    警告:没有共同提交
    远程:计数对象:3,完成。
    远程:总共 3 个(增量 0),重用 0 个(增量 0)
    拆开物体:100% (3/3),完成。
    来自../伽玛
     * [新分支]奴隶->伽玛/从属
    
    在回购测试版中:
    * 掌握
      遥控器/伽玛/从属
    
  • 克隆 beta 以制作 alpha< /代码>:

    $ git 克隆 beta alpha
    在 /tmp/test-git/alpha/.git/ 中初始化空 Git 存储库
    $ cd 阿尔法; git分支-a
    * 掌握
      遥控器/原点/HEAD ->起源/主人
      遥控器/原点/主控
    

现在变得激烈:

# 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/< /em>' 将拉入分支本身,但不会更新构成其所属远程的配置项。

有趣的是,为无远程分支设置跟踪分支将导致它开始跟踪自己的存储库:

$ 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文