某些场合,一个git项目需要能同时使用两个甚至多个远程仓库,比如国内+国外、测试环境+生产环境,等等。在项目的根目录查看git配置文件,一般来说是这样的:
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
hideDotFiles = dotGitOnly
[remote "origin"]
url = https://git.oschina.net/mvpboss1004/Availability.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
可见,我们常用的git remote add origin https://git.oschina.net/mvpboss1004/Availability.git中,origin只是个名字。
1. 添加一个远程仓库
修改config文件,加入另一个远程仓库,并为其命名,比如称为mirror:
[remote "origin"]
url = https://git.oschina.net/mvpboss1004/Availability.git
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "mirror"]
url = https://github.com/mvpboss1004/Availability.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
remote = mirror
merge = refs/heads/master
2. pull操作
使用以下命令,可以分别从两个远程仓库pull:
git pull origin master
git pull mirror master
3. push操作
使用以下命令,可以分别push到两个远程仓库:
git push origin master
git push mirror master