合并两个git库

308 阅读1分钟

背景

工作需要合并两个git库,之间没有文件重复,希望将两个库外面加一层目录,各分支保留历史记录地合并在一起
即两个子库(A库和B库)添加一层目录后合并成主库(C库)

首先迁移master分支

思路: git remote add添加两个库,通过--allow-unrelated-histories将子库的历史记录合并在一起

git clone https://github.com/sabersword/c-project.git
cd .\c-project\

# 处理Agit remote add a-project https://github.com/sabersword/a-project.git
git fetch a-project
git checkout -b a-branch a-project/main
mkdir a-project
mv a-module a-project
mv pom.xml a-project 
git add a-project
git commit -m  'adjust a-project directory'
git checkout main
git merge a-branch --allow-unrelated-histories

# 处理Bgit remote add b-project https://github.com/sabersword/b-project.git
git fetch b-project
git checkout -b b-branch b-project/main
mkdir b-project
mv b-module b-project
mv pom.xml b-project 
git add b-project
git commit -m  'adjust b-project directory'
git checkout main
git merge b-branch --allow-unrelated-histories

git push

以master为基础,迁移其他开发分支

思路: 以master为基础,将其他分支的代码强制覆盖过去,可能会丢失项目分支删除的文件,需要事后处理

git clone -b master https://github.com/sabersword/c-project.git
cd c-project

git push origin --delete c-dev
git checkout -b c-dev origin/master
git push origin c-dev

cd ..

git clone -b a-dev https://github.com/sabersword/a-project.git
cp -rf a-project/xxx ../c-project/a-project

git clone -b b-dev https://github.com/sabersword/b-project.git
cp -rf b-project/xxx ../c-project/b-project

cd c-project
git add a-prpject
git add b-project
git commit -m 'commit dev branch in a-project and b-project'
git push origin c-dev