一個項目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提交到多個遠程倉庫

相關文章
相關標籤/搜索