Persiapan repo yang mau digabungkan
cd RepoA (1) git checkout master (2) git pull (3) git checkout -b prepare_monorepo (4) mkdir -p repo-a (5) shopt -s extglob (6) git mv -k !(repo-a) repo-a (7) shopt -u extglob git rm -f --ignore-unmatch .gitattibutes (8) git rm -f --ignore-unmatch .gitignore (8) git rm -f --ignore-unmatch .editorconfig (8) git commit -m "Preparing for monorepo merge" (9)
- Switch to RepoA directory
- We can only merge one branch of both RepoA and RepoB, in this case master
- Update with all remote changes
- Create a new branch prepare_monorepo
- Create a subdirectory repo-a. This directory is the directory in which the content will end-up in MonoRepo.
- Extended globbing allows pattern matching
- Move everything into repo-a, excluding the directory repo-a itself
- Some files are not moved with the git mv command. .gitattributes , .gitignore and .editorconfig are a good example. Since we will have a complete new repository MonoRepo, it will have these files already. Therefore, we need to remove them from the old repository.
- Commit the change to RepoA. There is no need to push it to the remote, since we only use this change locally to merge in the next step.
Proses penggabungan ke repo baru
cd ../MonoRepo (1) git remote add -f RepoA ../RepoA (2) git merge -m "Integrating RepoA" RepoA/prepare_monorepo --allow-unrelated-histories (3) git remote rm RepoA (4) git push (5)
- Switch to the MonoRepo directory
- Add the directory ../RepoA as a remote with name RepoA
- Perform the actual merge with unrelated histories
- Remove the remote RepoA we created earlier
- Push the merge history to the origin remote of MonoRepo
Hapus branch sementara
cd ../RepoA git checkout --force master git branch -D prepare_monorepo cd ../
Now repeat all the steps for RepoB!
Sumber: jdriven.com/blog/2021/04/How-to-merge-multiple-git-repositories