git与多个远程仓库同步

在本地创建了一个Git仓库后,可以在GitHub上创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过该仓库来协作,真是一举多得。有时候,由于种种因素,远程仓库可能无法访问,这时候需要换一个仓库,当然最好的办法是同时与多个远程仓库同步。

已有项目更换远程仓库

更换一个远程仓库直接敲代码:

git remote add origin 远程仓库

但一般会出现错误fatal: remote origin already exists。解决办法是先删掉

$ git remote rm origin

也可以采用直接修改config文件的方法, 首先, 显示隐藏文件, 进入 .git/ 目录, 目录下面有一个config文件, 以文本文件方式打开该文件, 在后面添加

[remote "origin"]
url = 新地址

即可。

有时候仍然由错误,可以改个名字。然后使用git remote -v命令查看。

$ git remote add oschina http://git.oschina.net/sheyi.git
$ git push oschina master

$ git remote -v
origin    https://github.com/sheyi/higrid.git (fetch)
origin    https://github.com/sheyi/higrid.git (push)
oschina    http://git.oschina.net/sheyi/higrid.git (fetch)
oschina    http://git.oschina.net/sheyi/higrid.git (push)

这里将‘orign换成了‘oschina,然后在push到oschina时也是一样。

Git同时push到多个仓库

为防止一个git仓库由于各种原因造成无法访问,可以将代码push到多个仓库。 编辑本地仓库目录下面的.git目录下的config文件。

添加:

[remote "two"]
url = [email protected]:sheyi/higrid.git
url = [email protected]:sheyi/higrid.git

再push时,运行git push tow master

随机文章 Random Posts