一个项目push到多个远程Git仓库

我建立了一个项目,而后经过下面的命令 push 到了 GitHub 上。如何再将这个项目 push 到其余远程仓库呢?git

git remote add github https://github.com/zxbetter/test.git
git push -u github master

方法一: 使用 git remote add 命令

1.1# 以下命令查看远程仓库的状况,能够看到只有一个叫 github 的远程仓库。github

git remote
github

git remote -v
github  https://github.com/zxbetter/test.git (fetch)
github  https://github.com/zxbetter/test.git (push)

1.2# 使用以下命令再添加一个远程仓库(这里以码云为例)fetch

git remote add oschina https://git.oschina.net/zxbetter/test.git

1.3# 再次查看远程仓库的状况,能够看到已经有两个远程仓库了。而后再使用相应的命令 push 到对应的仓库就好了。这种方法的缺点是每次要 push 两次。url

git remote
github
oschina

git remote -v
github  https://github.com/zxbetter/test.git (fetch)
github  https://github.com/zxbetter/test.git (push)
oschina https://git.oschina.net/zxbetter/test.git (fetch)
oschina https://git.oschina.net/zxbetter/test.git (push)

方法二: 使用 git remote set-url 命令

2.1# 删除方法一的 oschina 远程仓库。.net

git remote rm oschina

2.2# 使用以下命令添加远程仓库。code

git remote set-url --add github https://git.oschina.net/zxbetter/test.git

2.3# 查看远程仓库状况。能够看到 github 远程仓库有两个 push 地址。这种方法的好处是每次只须要 push 一次就好了。blog

git remote -v
github  https://github.com/zxbetter/test.git (fetch)
github  https://github.com/zxbetter/test.git (push)
github  https://git.oschina.net/zxbetter/test.git (push)

方法三: 修改配置文件

打开 .git/config 找到 [remote "github"],添加对应的 url 便可,效果以下。这种方法其实和方法二是同样的。rem

[remote "github"]
    url = https://github.com/zxbetter/test.git
    fetch = +refs/heads/*:refs/remotes/github/*
    url = https://git.oschina.net/zxbetter/test.git

关于 git pull

方法二和三在 push 的时候比较方便。可是在 pull 的时候只能从方法三中的第一个 url 地址拉取代码。而方法一则不存在这种问题(可能要解决冲突)。
因此,若是只进行 push 操做,推荐方法二和三,若是也要进行 pull 操做,推荐方法一。get

参考

将项目提交到两个git仓库(github和oschina)it

Git提交到多个远程仓库

相关文章
相关标签/搜索