跳至主要內容

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

AI悦创原创GitGit大约 3 分钟...约 801 字

你好,我是悦创。

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

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

方法一: 使用 git remote add 命令

  1. 如下命令查看远程仓库的情况,可以看到只有一个叫 github 的远程仓库。
git remote
github

git remote -v
github  https://github.com/zxbetter/test.git (fetch)
github  https://github.com/zxbetter/test.git (push)
  1. 使用如下命令再添加一个远程仓库(这里以码云为例)
git remote add oschina https://git.oschina.net/zxbetter/test.git
  1. 再次查看远程仓库的情况,可以看到已经有两个远程仓库了。然后再使用相应的命令 push 到对应的仓库就行了。这种方法的缺点是每次要 push 两次。
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 命令

  1. 删除方法一的 oschina 远程仓库。
git remote rm oschina
  1. 使用如下命令添加远程仓库。
git remote set-url --add github https://git.oschina.net/zxbetter/test.git
  1. 查看远程仓库情况。可以看到 github 远程仓库有两个 push 地址。这种方法的好处是每次只需要 push 一次就行了。
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 即可,效果如下。这种方法其实和方法二是一样的。

[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 操作,推荐方法一。

想請問 git remote -v 之後, url 後面的 (fetch)(push) 是什麼意思?

fetch 说明你能拉取代码,push 说明你能把代码推送到远程。

欢迎关注我公众号:AI悦创,有更多更好玩的等你发现!

公众号:AI悦创【二维码】

AI悦创·编程一对一

AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh

C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh

方法一:QQopen in new window

方法二:微信:Jiabcdefh

上次编辑于:
贡献者: AndersonHJB,AI悦创
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度