Git设置多远程仓库
我有下面两个仓库:
https://gitee.com/fsoooo/test.git
https://github.com/fsoooo/test.git
方法一: 使用 git remote add 命令
1.1 添加多个远程仓库
先添加第一个仓库:
git remote add origin https://gitee.com/fsoooo/test.git
再添加第二个仓库:
git remote set-url --add origin https://github.com/fsooo/test.git
如果还有其他,则可以像添加第二个一样继续添加其他仓库。
然后使用下面命令提交:
git push origin --all
打开.git/config,可以看到这样的配置:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = https://gitee.com/wangslei/test.git
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://github.com/fsoooo/test.git
[branch "master"]
remote = origin
merge = refs/heads/master
刚才的命令其实就是添加了这些配置。如果不想用命令行,可以直接编辑该文件,添加对应的url即可。
1.2 查看远程仓库的情况
git remote
origin
git remote
origin https://gitee.com/fsoooo/test.git (fetch)
origin https://gitee.com/fsoooo/test.git (push)
1.3 查看远程仓库情况。可以看到 github 远程仓库有两个 push 地址。
git remote -v
gitee https://gitee.com/fsoooo/test.git (fetch)
gitee https://gitee.com/fsoooo/test.git (push)
gitee https://github.com/fsoooo/test.git (push)
1.4 推送远程分支
这种方法的好处是每次只需要 push 一次就行了
方法二: 修改配置文件**
2.1 添加配置模块
打开 .git/config 找到 [remote],添加对应的 模块 即可,效果如下:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = https://gitee.com/wangslei/test.git
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "github"]
url = https://github.com/fsoooo/test.git
fetch = +refs/heads/*:refs/remotes/github/*
[branch "master"]
remote = origin
merge = refs/heads/master
2.2 查看远程仓库的情况
git remote
origin
github
git remote
github https://github.com/fsoooo/test.git (fetch)
github https://github.com/fsoooo/test.git (push)
2.3 再次查看远程仓库的情况,可以看到已经有两个远程仓库了。然后再使用相应的命令 push 到对应的仓库就行了。
git remote
github
gitee
git remote -v
github https://github.com/fsoooo/test.git (fetch)
github https://github.com/fsoooo/test.git (push)
gitee https://gitee.com/fsoooo/test.git (fetch)
gitee https://gitee.com/fsoooo/test.git (push)
2.4 推送远程分支
这种方法的缺点是每次要 push 两次
git push origin master
git push github master
关于 git pull
方法一在 push 的时候比较方便。但是在 pull 的时候只能从方法一中的第一个 url 地址拉取代码。而方法二则不存在这种问题(可能要解决冲突)。
所以,如果只进行 push 操作,推荐方法一,如果也要进行 pull 操作,推荐方法二。
参考文章
1、https://www.jianshu.com/p/833f33060909