为git同时添加两个远程仓库

一直使用GitHub作为自己git版本控制的远程仓库,但是最近觉得下载速度有些慢,太慢。所以决定将代码在国内git仓库放一个镜像,下载的时候可以提高速度。

经过搜索找到不错的方案。以下转载自知乎。

作者:燕南
链接:https://www.zhihu.com/question/28563469/answer/41284272
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

如果是 Git,一般来说最佳方法是给 origin 设两个地址:

  • Use case 1: 多地址的 remote repo:
git remote set-url origin --add https://github.com/USERNAME/REPO1.git
git remote set-url origin --add https://github.com/USERNAME/REPO2.git

在 .git/config 里得到

...
[remote "origin"]
    url = https://USERNAME@github.com/USERNAME/REPO1.git
    url = https://USERNAME@github.com/USERNAME/REPO2.git
...
[branch "master"] 
    remote = origin
...
然后
git push origin master
就会同时提交到两个 repo,而
git pull origin master

会从两个 repo 里取得更新。

当然 URL 和 repo 不一定非要是 GitHub 上的,具有两个 url 的 remote 也不一定要是 origin,比如可以设置成 all。
  • 只用于 push 的备份 repo

另外一种 use case,你想从 repo1 pull,但是 push 的时候要推送到 repo1 和另一个 repo2,

git remote set-url origin --add https://github.com/USERNAME/REPO1.git
git remote set-url origin --push --add https://example.com/USERNAME/REPO2.git

在 .git/config 里得到

...
[remote "origin"]
    url = https://USERNAME@github.com/USERNAME/REPO1.git
    pushurl = https://USERNAME@example.com/USERNAME/REPO2.git
...
[branch "master"] 
    remote = origin
...
然后
git push origin master
就会同时提交到两个 repo,而
git pull origin master
会从两个 GitHub repo1 里取得更新。
posted @ 2018-01-17 23:44  huipengly  阅读(982)  评论(0编辑  收藏  举报