3

本地 Git 仓库关联多个远程仓库的两种方法

 2 years ago
source link: https://www.zackwu.com/posts/2019-09-11-ways-to-add-multiple-remote-repos-for-one-local-git-repo/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

本地 Git 仓库关联多个远程仓库的两种方法

2019-09-11
  1. Git

通常情况下,一个本地 Git 仓库对应一个远程仓库,每次pullpush仅涉及本地仓库和该远程仓库的同步;然而,在一些情况下,一个本地仓库需要同时关联多个远程仓库,比如:同时将一个项目发布在 GithubCoding 上,以兼顾国内外的访客(顺便一提,本站从近期起即是如此)。

那么,如何让一个本地仓库同时关联多个远程仓库呢?

方法 1:每次pushpull时需分开操作

首先,查看本地仓库所关联的远程仓库:(假定最初仅关联了一个远程仓库)

$ git remote -v
origin  [email protected]:keithnull/keithnull.github.io.git (fetch)
origin  [email protected]:keithnull/keithnull.github.io.git (push)

然后,用git remote add <name> <url>添加一个远程仓库,其中name可以任意指定(对应上面的origin部分),比如:

$ git remote add coding.net [email protected]:KeithNull/keithnull.github.io.git

再次查看本地仓库所关联的远程仓库,可以发现成功关联了两个远程仓库:

$ git remote -v
coding.net      [email protected]:KeithNull/keithnull.github.io.git (fetch)
coding.net      [email protected]:KeithNull/keithnull.github.io.git (push)
origin  [email protected]:keithnull/keithnull.github.io.git (fetch)
origin  [email protected]:keithnull/keithnull.github.io.git (push)

此后,若需进行push操作,则需要指定目标仓库,git push <repo> <branch>,对这两个远程仓库分别操作:

$ git push origin master
$ git push coding.net master

同理,pull操作也需要指定从哪个远程仓库拉取,git pull <repo> <branch>,从这两个仓库中选择其一:

$ git pull origin master
$ git pull coding.net master

方法 2:pushpull无需额外操作

在方法 1 中,由于我们添加了多个远程仓库,在pushpull时便面临了仓库的选择问题。诚然如此较为严谨,但是在许多情况下,我们只需要保持远程仓库完全一致,而不需要进行区分,因而这样的区分便显得有些“多余”。

同样地,先查看已有的远程仓库:(假定最初仅关联了一个远程仓库)

$ git remote -v
origin  [email protected]:keithnull/keithnull.github.io.git (fetch)
origin  [email protected]:keithnull/keithnull.github.io.git (push)

然后,不额外添加远程仓库,而是给现有的远程仓库添加额外的 URL。使用git remote set-url -add <name> <url>,给已有的名为name的远程仓库添加一个远程地址,比如:

$ git remote set-url --add origin [email protected]:KeithNull/keithnull.github.io.git

再次查看所关联的远程仓库:

$ git remote -v
origin  [email protected]:keithnull/keithnull.github.io.git (fetch)
origin  [email protected]:keithnull/keithnull.github.io.git (push)
origin  [email protected]:KeithNull/keithnull.github.io.git (push)

可以看到,我们并没有如方法 1 一般增加远程仓库的数目,而是给一个远程仓库赋予了多个地址(或者准确地说,多个用于push的地址)。

因此,这样设置后的pushpull操作与最初的操作完全一致,不需要进行调整。

以上是给一个本地仓库关联多个远程仓库的两种方法,二者各有优劣,不过出于简便考虑,我最终采用了方法 2。

此外,上述内容中涉及到的 Git 指令略去了许多不常用的参数,如需更加详细的说明,可以查阅 Git 文档,或者直接在命令行运行git remote --help


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK