【问题标题】:Use case for pushing remote branches to another remote repo in git将远程分支推送到 git 中的另一个远程仓库的用例
【发布时间】:2019-03-17 05:01:55
【问题描述】:

在 git 中,我们可以像这样将一个远程分支推送到另一个远程:

> git clone git@foo1.com:a/a
> cd a
> git remote add other git@foo2.com:b/b
> git fetch --all
# ...
> git push origin other/master
Counting objects: 107, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (66/66), done.
Writing objects: 100% (107/107), 2.11 MiB | 0 bytes/s, done.
Total 107 (delta 52), reused 87 (delta 33)
remote: Resolving deltas: 100% (52/52), completed with 8 local objects.
To foo1.com:a/a
 * [new branch]        other/master -> other/master

所以在“@foo1.com:a/a”上,我们可以看到它得到了“foo2.com:b/b”的远程,通过使用git branch -va命令:

* master                  7c6051f foo
  remotes/other/master    38a5a1b bar

如果我们只使用git branch -v,这个远程分支就不会出现。

但是我想不出这有什么用。我的本地人无法在foo1.com:a/a 上与此进行交互,对吗?有人可以为此提供一个潜在的用例吗?

也可以通过一些 git 配置以某种方式禁用它吗?

【问题讨论】:

    标签: git branch git-branch


    【解决方案1】:

    所以在“@foo1.com:a/a”上,我们可以看到它得到了“foo2.com:b/b”的远程,通过使用git branch -va命令:

    不,你不知道:git branch -av 纯粹是一个 local 命令,它列出了在本地 repo 中完成的本地分支。
    该分支remotes/other/master 不在您的远程仓库foo1.com:a/a 上。只有master 是。

    remotes/other/master 是一个本地“远程跟踪分支”,旨在让您的本地存储库记住分支“master”的远程存储库“other”推送或提取的内容。

    git push origin other/master
    

    这确实会创建另一个名为 other/master 的分支到远程仓库 origin (foo1.com)

    但您通常不需要这样做:您在本地远程跟踪分支 other/master 和您自己的 master 之间进行本地合并,然后使用简单的 git push 推送 master 本身。

    【讨论】:

    • 我在“@foo1.com:a/a”远程服务器的存储库上运行了git branch -va,它确实在那里,为 b/b 的主服务器提供了正确的 SHA
    • 谢谢,所以我猜你也认为这没有用例,可以被禁止而没有副作用?
    • @lulalala 是的,这不是一个常见的用例,但它不能轻易被禁止:stackoverflow.com/a/30471886/6309
    猜你喜欢
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 2011-09-10
    • 2016-07-08
    相关资源
    最近更新 更多