美文网首页
Git命令 - git多个远程仓库

Git命令 - git多个远程仓库

作者: BlueSocks | 来源:发表于2023-07-20 11:20 被阅读0次

由于GitHub太慢,想再找个国内类似GitHub的代码托管平台,同时也还想持续更新GitHub上的仓库,于是需要一个本地仓库以及多个远程仓库(Github、码云、coding)。

1.准备条件:创建git本地仓库

git init
touch test.txt
git add test.txt
git commit -m "Fitst commit"

2. 方法一:添加多个远程地址

  1. 添加多个远程仓库
//添加github远程仓库,名称我们定义为origin
git remote add origin <github仓库url>

//添加gitlab远程仓库,远端名称我们定义为mirror
git remote add mirror <gitlab仓库url>

  1. 拉取github仓库代码
git pull origin main

如果,在Github创建仓库时,默认添加了README.md文件,拉取代码会失败,报错信息如下:

hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

造成该问题的原因就是:本地仓库执行了一次commit,提交了test.txt文件;Github创建仓库时默认也执行了一次commit,提交了README.md文件;这两次提交没有任何关系,我们需要告诉git如何处理这两次提交。

然后,我们根据提示,使用如下命令拉取:

//pull时使用rebase方式合并不同的commit
git pull origin main --rebase

  1. 拉取gitlab仓库代码

如果在gitlab创建仓库时,也默认添加了README.md文件,git pull mirror main拉取代码同样会失败,并且此时我们不可以再使用--rebase命令了,而是需要merge:

//如果直接merge还是会失败, 执行如下命令:
$ git merge mirror/main
//git会告诉我们:拒绝合并不相关的历史。报错信息如下:
fatal: refusing to merge unrelated histories

直接merge会失败,我们还需要加上--allow-unrelated-histories 参数,允许不相关的历史合并:

git merge origin/main --allow-unrelated-histories

合并后,可能会出现冲突,即README.md文件冲突,需要我们打开READEM.md文件解决冲突,然后重新提交README.md文件。之后就可以在两个远程仓库中拉取、推送代码了:

//拉取github仓库代码
git pull origin main
//向github仓库中推送代码
git push origin main

//拉取gitlab仓库代码
git pull mirror main
//向gitlab仓库中推送代码
git push mirror main

3. 方法二:添加多个url

如果github和gitlab创建仓库时都没有添加README.md文件,都是一个空仓库,可以使用更简单的用法:

  1. 添加github远程仓库
git remote add origin <github仓库url>

  1. 在origin中添加gtilab仓库的url
git remote set-url --add origin <gitlab仓库url>

  1. 推送并建立远端映射
git push --set-upstream origin main

  1. 之后一次推送可以更新多个远程仓库
git push origin main
//or
git push

相关文章

  • 每日小结2.21

    git常用指令汇总 初始化本地仓库 命令:git init 克隆远程仓库 命令:git clone 远程仓库 添加...

  • Git 入门到放弃

    简介 git关联远程仓库 本地仓库与远程仓库同步问题 Git 终端命令 git关联远程仓库 基本流程 注册gith...

  • git 常用命令

    远程仓库相关命令 检出仓库:git clone git://github.com/xxx/xxx.git 查看远程...

  • Git 命 令 大 全

    Git常用操作命令: 1) 远程仓库相关命令 检出仓库:$ git clone git://github[http...

  • git命令大全

    Git常用操作命令: 远程仓库相关命令检出仓库:$ git clone git://github.com/jque...

  • git学习笔记

    Git常用操作命令: 1. 远程仓库相关命令 检出仓库:$ git clone git://github.com/...

  • git 命令指南

    整理一下 git 命令,用时总会忘记。 Git常用命令 git 克隆远程分支仓库git 克隆远程仓库项目时如果不指...

  • git 的相关命令

    git 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/...

  • git命令

    Git常用操作命令: 1)远程仓库相关命令 检出仓库:$ git clone git://github.com/j...

  • GIT使用总结

    GIT的基本命令 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquer...

网友评论

      本文标题:Git命令 - git多个远程仓库

      本文链接:https://www.haomeiwen.com/subject/wwxzudtx.html