Git-合并两个不同的仓库


背景:由于拆分微前端,需要将最新代码合并到已经拆分的微前端项目,即需要将 2 个项目合并。

1.git 合并两个不同的仓库必备知识

1>.列出本地已经存在的分支

git branch

2>.查看当前 git 关联的远程仓库

git remote -v

3>.解除当前仓库关联的远程仓库

git remote remove origin  //origin 远程分支别名

4>.git 同时关联多个远程仓库

//当前默认仓库别名为origin
git remote add origin http://km-git1.kemai.cn/youshu/youshu-smallshop-frontend.git

//其他仓库别名为other
git remote add other http://km-git1.kemai.cn/youshu/xdd-front-end.git

5>.从远程切出新分支

//从默认的远程仓库切出一个新分支
 git checkout -b master origin/master

//从其他的远程仓库切出一个新分支(
//注意同一个仓库中不能存在2个同名分支,所以取个别名,但是同一个仓库中不同的分支可以关联多个远程仓库。
//一个本地分支只能关联一个远程仓库
git checkout -b other other/master

6>.切换分支

//切换到关联默认仓库远程master分支的本地分支master
git checkout master

//切换到关联其他仓库远程master分支的本地分支other
git checkout other

7>.更新仓库

git fetch --all //更新所有仓库

git fetch origin //更新指定仓库
git fetch other //更新指定仓库

8>.更新本地分支

/**更新主仓库代码**/
//先切换到other
git checkout master
//从默认的远程仓库更新
git pull origin master


/**更新子仓库代码**/
//先切换到other
git checkout other
//从其他的远程仓库更新
git pull other master

9>.推送代码

//推送到默认的远程仓库
git push -u origin master

//推送到其他的远程仓库
git push -u other master

10>.删除分支

git branch -D XXX  //强制删除

11>.重名分支

git branch -m  <branch>

12>.git 基础命令大全

更多基础命令请查看 https://www.yuque.com/docs/share/8bd713d2-0bc8-4758-85ba-5cb6ca764b92?# 《常见的 git 命令》

2.实际操作

1.项目仓库

现在有两个仓库 [leader/](https://www.leader755.com)youshu-smallshop-frontend (主仓库) 和 [leader/](https://www.leader7555.com)xdd-front-end(子仓库) 我们需要将 xdd-front-end 仓库合并到 youshu-smallshop-frontend 并保留xdd-front-end  的所有提交内容。
  • 主仓库: youshu-smallshop-frontend
  • 子仓库: xdd-front-end

2. 克隆主仓库代码(youshu-smallshop-frontend)

git clone http://km-git1.kemai.cn/youshu/youshu-smallshop-frontend.git

//git remote add origin http://km-git1.kemai.cn/youshu/youshu-smallshop-frontend.git

3. 将 (子仓库) 作为远程仓库,添加到 (主仓库) 中,设置别名为 other

git remote add other http://km-git1.kemai.cn/youshu/xdd-front-end.git

git remote -v

image.png

4.将(子仓库) 中拉取数据到本地仓库

git fetch other

5.将 (子仓库) 拉取的 master 分支作为新分支 checkout 到本地,新分支名设定为 other

git checkout -b other other/master

6.切换回 (主仓库) 的 master 分支

git checkout master

7.更新本地分支

/**更新主仓库代码**/
//先切换到other
git checkout master
//先切换到other
git checkout other
//从其他的远程仓库更新
git pull other master

8.将 xdd-front-end(子) 合并入 youshu-smallshop-frontend(主) 的 master 分支

git merge other
# 如果第 7 步报错 `fatal: refusing to merge unrelated histories`
# 请执行下面命令 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
git merge other --allow-unrelated-histories

在合并时有可能两个分支对同一个文件都做了修改,这时需要解决冲突,对文本文件来说很简单,根据需要对冲突的位置进行处理就可以。对于二进制文件,需要用到如下命令:

git checkout --theirs YOUR_BINARY_FILES     # 保留需要合并进来的分支的修改
git checkout --ours YOUR_BINARY_FILES       # 保留自己的修改
git add YOUR_BINARY_FILES

9.参考文章:

[https://segmentfault.com/a/1190000021919753](https://segmentfault.com/a/1190000021919753)

若有收获,就点个赞吧


文章作者:   leader755
版权声明:   本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 leader755 !
评论
 上一篇
Parallels Desktop 18 for Mac破解版 Parallels Desktop 18 for Mac破解版
1.parallels desktop 18 破解版安装教程:链接下载:链接:https://cloud.hiksemi.cn/#share-UO0HOE,提取码:3149 先安装包内的 pd18 关闭更新和发送体验报告,退出 para
2022-09-24
下一篇 
VConsole—Vue项目 VConsole—Vue项目
VConsole——Vue 项目中使用手机网页的前端 console 调试面板1.cdn 使用 使用 cdn 引入,在 public 目录下的 index.html 中引入如下代码即可 <body> <script
2022-05-14
  目录