多modules git仓库迁移并保留历史记录

661 阅读1分钟

从一个git 仓库 迁移多个modules 到另一个 git仓库并保留历史记录

网上所见多是迁移单个module,如果存在module和module提交耦合会出现冲突的问题

需求: 将repoB 中的 bc/bd/be/bf 迁移到 repoA 中并保留bc/bd/be/bf 文件夹中对应文件的commit history

  1. 新建文件夹clone repoB
git clone 'url of from repo(repoB)' repoB
cd repoB
git remote rm origin

(为了避免影响之前local repo 的内容,建议新建文件夹clone操作)

2.保留新建文件夹中需要迁移的 bc/bd/be/bf 这四个文件夹对应文件的历史记录,其他文件历史记录移除

git filter-branch --index-filter 'git rm --cached -qr --ignore-unmatch -- . && git reset -q $GIT_COMMIT -- bc bd be bf' --prune-empty -- --all
git reset --hard
cd ..

3.类似1 新建文件夹 clone repoA

git clone 'url of to repo(repoA)' repoA
cd repoA

4.关联2中处理后的repoB 对应branch 到 repoA 的branch(关联branch 比如:biz/repoB_migration)

git remote add repoB(full path of repoB from 1)
git pull repoB 'biz/repoB_migration'--allow-unrelated-histories [force building connection] )
git remove rm repoB
  1. 提交
git commit -a -m "xxxx"
git push origin biz/repoB_migration

6.清理之前1/3中的文件操作

rm -rf repoB
rm -rf repoA

ps:如需要将对应文件夹bc从repoA 的根目录 迁移到 根目录下的某个X文件夹 可执行:

git mv bc(full path) git mv X/bc
...

参考实现: www.blyth.me.uk/2017/06/07/…