Git两库合并历史记录

Git两库合并历史记录

情景

有两个库, A与B
需要合并两个库的代码并保留完整的记录

思路

两库合并的重点在于,在原本一个库上加上另一个库.
实际操作主要的点在remote add, 关联多的一个库
之后再进行,检出与新库关联的新分支,把新分支与老分支进行合并.

要点

  1. remote add
  2. 两库对应分支的合并方式, 是merge还是rebase
  3. 关于合并记录的先后顺序

操作步骤(单分支)

  1. 检出第1个库
# git clone <repo1-url>
git clone http://192.168.1.1/root/A-project.git
  1. 添加库与项目
# git remote add <repo2-name> <repo2-url>
git remote add other http://192.168.1.1/root/B-project.git
  1. 获取新旧项目与远程地址的更新
git fetch --all
  1. 检出一个新的本地分支, 用来与远程分支对应
# git checkout -b <repo2-branchName> remotes/<repo2-name>/<repo2-branchName> 
git checkout -b otherMaster remotes/other/master
  1. 合并两个分支
# 此处, 可以基于提交数较少的分支, 把相较而言更新的分支合并进当前分支. 此处, 我基于otherMaster,把master分支合并入该分支,因为master的记录更多且更新
# git merge <repo1-branchName> --allow-unrelated-histories
git merge master --allow-unrelated-histories
原文地址:https://www.cnblogs.com/jrri/p/15682504.html