git合并两个不同的仓库

时间:2023-03-08 23:04:44
git合并两个不同的仓库

目前开发是2个仓库,线上仓库online_a(对应的branch分支为online),测试环境online_b(对应的branch分支为demo),测试环境需要时刻保持onine_a上的最新稳定稳定代码同步过来。如何合并呢?特此记录下:
在测试仓库onine_b 上执行:

1:测试仓库添加远程生产仓库(切换到自己的测试仓库下执行以下命令,比如我的当期测试online_b.git)

git remote add online_a git@github.com:fantasy/online_a.git //将online_a作为远程仓库,添加到online_b中,设置别名为online_a(自定义,这里我是为了方便区分仓库名)

2:从远程仓库下载,这时我们弄个新的

git fetch online_a //从online_a仓库中抓取数据到本仓库,注意这里是上面设置的别名
$ git fetch online-a
remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta )
Receiving objects: % (/), 2.44 MiB | 214.00 KiB/s, done.
Resolving deltas: % (/), completed with local objects.
From git@github.com:fantasy/online_a.git* [new branch] demo -> online-backend/demo
* [new branch] online -> online-backend/online
* [new tag] demo-last-bad -> demo-last-bad
* [new tag] demo-last-ok -> demo-last-ok
* [new tag] v2.-beta -> v2.-beta
* [new tag] v2.-days -> v2.-days
* [new tag] v2.-dist -> v2.-dist
* [new tag] v2.-dist -> v2.-dist
* [new tag] v2.-nosmartbid -> v2.-nosmartbid
* [new tag] v2.2demo -> v2.2demo
* [new tag] v2.-bad-smartbid -> v2.-bad-smartbid
* [new tag] demo-no-score -> demo-no-score
* [new tag] tmp-repay-v1 -> tmp-repay-v1
* [new tag] tmp-repay-v2 -> tmp-repay-v2
* [new tag] transfer-dep-last -> transfer-dep-last
* [new tag] transfer-dep-ok -> transfer-dep-ok

3:将online_a仓库抓去的online分支作为新分支checkout到本地,新分支名设定为online_repo1

$ git checkout -b online_repo1 online-a/online  //注意这里也是别名online_a
Switched to a new branch 'online_repo1'
Branch 'online_repo1' set up to track remote branch 'online' from 'online-a'.

4:切换回本地测试的online_b的demo分支

$ git checkout demo
Switched to branch 'demo'
Your branch is up to date with 'origin/demo'.

5:将online_repo1合并入demo分支

git merge online_repo1

6:解决冲突:

git add .
git commit -m "合并"
git push online_repo1 online_a:online //上传到远程库
git checkout demo
git merge online_repo1
git branch -d online_repo1
总结:
  1. 大致思路是伪造远程的repo1仓库为repo2的一个分支,然后合并进来;
  2. 若是文件有冲突、或要建立子目录,建议在repo1中先解决,再进行如上操作。