Skip to content

git 多远程仓库部署

本文主要讲述在平时开发中,一份代码可能有多份远程仓库的情况下,需要更新到不同的仓库 以及 同步更新到所有的仓库

手动添加多个 Remote 并分别推送

1.本地创建好项目文件夹2.打开终端, cd进入 项目文件夹 , 然后初始化

shell
git init

2.查看当前远程仓库

sh
git remote -v

3.添加仓库地址

shell
git remote add github https://github.com/loveagri/curl.git
git remote add gitlab https://gitlab.com/loveagri/curl.git
git remote add gitee https://gitee.com/loveagri/curl.git

4.查看关联的仓库地址情况

shell
git remote -v

github  https://github.com/loveagri/curl.git (fetch)
github  https://github.com/loveagri/curl.git (push)
gitlab  https://gitlab.com/loveagri/curl.git (fetch)
gitlab  https://gitlab.com/loveagri/curl.git (push)
gitee   https://gitee.com/loveagri/curl.git (fetch)
gitee   https://gitee.com/loveagri/curl.git (push)

5.add文件

shell
git add .

6.commit

shell
git commit -m"提交版本描述"

7.提交到仓库

shell
git push github master:<remote branch name>
git push gitlab master:<remote branch name>
git push gitee master:<remote branch name>

8.拉取代码

shell
git pull github master
git pull gitlab master
git pull gitee master

一次性推送到所有 Remote(推荐)

1.先设置默认远程仓库并设置额外远程推送仓库

shell
git remote add origin git@github.com:loveagri/curl.git

# --push:用于 修改或指定唯一的推送 URL(覆盖原有配置)。
# --add:用于 追加推送 URL(实现一推多仓库)。
git remote set-url --add  origin git@gitee.com:loveagri/curl.git
git remote set-url --add  origin git@gitcode.com:loveagri/curl.git

2.添加额外远程仓库

shell
git remote -v 

origin	git@github.com:loveagri/curl.git (fetch)
origin	git@github.com:loveagri/curl.git (push)
origin	git@gitee.com:loveagri/curl.git (push)
origin	git@gitcode.com:loveagri/curl.git (push)

3.一次性提交

shell
git add .
git commit -m"提交版本描述"
git push -u origin main # -u  === --set-upstream