git:一个项目push到多个远程仓库

创建了一个项目,然后通过下面的命令 push 到了 GitHub 上。如何再将这个项目 push 到其他远程仓库呢?

1
2
git remote add github https://github.com/zhuanli/test.git
git push -u github master

  • 1.使用 git remote add 命令
1
2
3
4
5
6
7
git remote
github

git remote -v
github https://github.com/zhuanli/test.git (fetch)
github https://github.com/zhuanli/test.git (push)
git remote add oschina https://git.oschina.net/zhuanli/test.git
  • 2.使用 git remote set-url 命令
1
2
3
4
5
6
git remote rm oschina
git remote set-url --add github https://git.oschina.net/zhuanli/test.git
git remote -v
github https://github.com/zhuanli/test.git (fetch)
github https://github.com/zhuanli/test.git (push)
github https://git.oschina.net/zhuanli/test.git (push)
  • 3.修改配置文件

打开 .git/config 找到 [remote “github”],添加对应的 url 即可.

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

2和3在 push 的时候比较方便。但是在 pull 的时候只能从第一个 url 地址拉取代码。而1则不存在这种问题(可能要解决冲突)。