1. 前言
- 转载请注明出处
- 本文主要解决一个本地仓库对应多个远程仓库的问题
- 本文主要在局域网的服务器上的gitlab做验证,验证结果可用于各种公共代码托管平台
- 本文约 5000字,大约需要7分钟
2. 简介
本文主要解决以下两个需求
- 有时候我们需要把我们的本地仓库托管到两个远程仓库,例如,国内gitee和国外github
- 有时候我们需要让两个远程仓库保有不同的代码版本,例如,工作时,公司服务器上的一些机密代码,我们并不能保存到我们的个人github仓库
3. 方法一:镜像push到多个远程仓库
3.1 准备工作
- 准备两个空的远程仓库:mirror_rep_1 和 mirror_rep_2, 如下图所示
- 准备一个空的本地仓库:mirror_local, 如下图所示
3.2 现有仓库示意图
- 空的本地仓库 mirror_local 如下所示
- 空的远程仓库1 mirror_rep_1 如下所示
- 空的远程仓库1 mirror_rep_2 如下所示
3.3 绑定两个远程仓库到本地仓库
- 通过以下指令添加远程仓库 mirror_rep_1 到本地仓库 mirror_local
$ git remote add mirror_rep_1 http://192.168.1.24/test/mirror_rep_1.git
- 通过以下指令添加远程仓库 mirror_rep_2 到本地仓库 mirror_local
$ git remote add mirror_rep_2 http://192.168.1.24/test/mirror_rep_2.git
- 通过打卡本地仓库下的
.git/config 文件,查看是否配置成功,如下图所示
3.4 推送本地仓库到两个远程仓库
- 输入以下指令序列推送本地仓库 mirror_local 中的
share_doc.txt 两个远程仓库
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (master)
$ touch share_doc.txt
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (master)
$ ls -l
total 0
-rw-r--r-- 1 Jon 197121 0 Nov 24 14:52 share_doc.txt
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (master)
$ git add .
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (master)
$ git commit -m 'mirror push to 2 remote reps'
[master (root-commit) f3f68ed] mirror push to 2 remote reps
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 share_doc.txt
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (master)
$ git push mirror_rep_1 master
info: detecting host provider for 'http://192.168.1.24/'...
info: detecting host provider for 'http://192.168.1.24/'...
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 232 bytes | 232.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To http://192.168.1.24/test/mirror_rep_1.git
* [new branch] master -> master
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (master)
$ git push mirror_rep_2 master
info: detecting host provider for 'http://192.168.1.24/'...
info: detecting host provider for 'http://192.168.1.24/'...
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 232 bytes | 232.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To http://192.168.1.24/test/mirror_rep_2.git
* [new branch] master -> master
- 通过 vscode 自带的git graph管理工具可以检查是否推送成功,如下图所示
- 也可以通过访问两个远程仓库的来检查是否更新成功,如下图所示
4. 方法二:镜像push到多个远程仓库
4.1 绑定两个远程仓库到本地仓库
- 准备工作参考 3.1 和 3.2 两个章节,这里省略不讲
- 通过以下指令添加远程仓库 **mirror_rep_1 和 mirror_rep_1 ** 到本地仓库 mirror_local_2
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local_2 (master)
$ git remote add origin http://192.168.1.24/test/mirror_rep_1.git
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local_2 (master)
$ git remote set-url --add origin http://192.168.1.24/test/mirror_rep_2.git
- 通过打卡本地仓库下的
.git/config 文件,查看是否配置成功,如下图所示
4.4 推送本地仓库到两个远程仓库
- 输入以下指令序列推送本地仓库 mirror_local_2 中的
share_doc_2.txt 两个远程仓库
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local_2 (master)
$ git remote add origin http://192.168.1.24/test/mirror_rep_1.git
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local_2 (master)
$ git remote set-url --add origin http://192.168.1.24/test/mirror_rep_2.git
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local_2 (master)
$ git pull origin master
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), 212 bytes | 35.00 KiB/s, done.
From http://192.168.1.24/test/mirror_rep_1
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local_2 (master)
$ code .
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local_2 (master)
$ touch share_doc_2.txt
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local_2 (master)
$ ls -l
total 0
-rw-r--r-- 1 Jon 197121 0 Nov 24 15:32 share_doc.txt
-rw-r--r-- 1 Jon 197121 0 Nov 24 15:33 share_doc_2.txt
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local_2 (master)
$ git add .
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local_2 (master)
$ git commit -m 'upload share_doc_2.txt to two reps'
[master 171a6f5] upload share_doc_2.txt to two reps
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 share_doc_2.txt
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local_2 (master)
$ git push origin --all
info: detecting host provider for 'http://192.168.1.24/'...
info: detecting host provider for 'http://192.168.1.24/'...
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 269 bytes | 269.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
To http://192.168.1.24/test/mirror_rep_1.git
f3f68ed..171a6f5 master -> master
info: detecting host provider for 'http://192.168.1.24/'...
info: detecting host provider for 'http://192.168.1.24/'...
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 269 bytes | 269.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
To http://192.168.1.24/test/mirror_rep_2.git
f3f68ed..171a6f5 master -> master
- 通过 vscode 自带的git graph管理工具可以检查是否推送成功,如下图所示
3. 也可以通过访问两个远程仓库的来检查是否更新成功,如下图所示
5. 对多个远程仓库push不同的代码版本
5.1 准备
- 准备工作,参考 3.1 和 3.2 和 private2
- 思路:通过使用不同的默认分支名实现对不同的仓库的分开管理
- 修改 **mirror_rep_2 ** 的默认远程分支为 rep2 并删除 master 主分支如下所示
- 创建本地仓库的新分支,这一名称需和绑定的远程仓库分支保持一致,使用如下指令
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (master)
$ git branch rep2
$ git checkout rep2
Switched to branch 'rep2'
5.2 推送本地仓库到远程仓库2
- 在 rep2 分支下创建 新文件 private_rep_2.txt 并推送到远程仓库2
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (rep2)
$ touch private_rep_2.txt
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (rep2)
$ git add .
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (rep2)
$ git commit -m 'upload new file private_rep_2.txt to remote rep2'
[rep2 bd35ca3] upload new file private_rep_2.txt to remote rep2
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 private_rep_2.txt
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (rep2)
$ git push mirror_rep_2 rep2
info: detecting host provider for 'http://192.168.1.24/'...
info: detecting host provider for 'http://192.168.1.24/'...
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 273 bytes | 273.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
To http://192.168.1.24/test/mirror_rep_2.git
0640b32..bd35ca3 rep2 -> rep2
5.3 推送本地仓库到远程仓库1
- 在master分支下,创建新文件 private_rep_1.txt, 并推送到远程仓库2
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (master)
$ git checkout rep2
Switched to branch 'rep2'
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (rep2)
$ touch private_rep_2.txt
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (rep2)
$ git add .
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (rep2)
$ git commit -m 'upload new file private_rep_2.txt to remote rep2'
[rep2 bd35ca3] upload new file private_rep_2.txt to remote rep2
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 private_rep_2.txt
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (master)
$ git add .
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (master)
$ git commit -m 'upload new file private_rep_1.txt to remote rep1'
[master 6b52106] upload new file private_rep_1.txt to remote rep1
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 private_rep_1.txt
Jon@Scorpione MINGW64 /f/Job/Job_ZhongKe/mirror_local (master)
$ git push mirror_rep_1 master
info: detecting host provider for 'http://192.168.1.24/'...
info: detecting host provider for 'http://192.168.1.24/'...
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 294 bytes | 294.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
To http://192.168.1.24/test/mirror_rep_1.git
171a6f5..6b52106 master -> master
5.4 检验结果
- 通过vscode 自带的 git graph 来查看
- 也可以通过登陆远程仓库查看
6. 总结
- 当我们要镜像推送时,方法一管理会更灵活一些,而方法二会更省力一些
- 当我们要推送不同的版本时,一定要注意在push时,要切换到对应的分支下,并选择好远程仓库以及对应的分支名,远程仓库的分支名和本地仓库的分支名一定要保持一致!!!否则会造成推送不成功,或者创建了一个新的分支名在远程仓库
6. 参考文献
- git本地仓库推送到多个远程仓库
|