Struggling with merging multiple repositories together into one (mono) repository? You wanna preserve your valuable git history?
This blogpost will show you how to do this step-by-step.
Let’s assume we have two repositories.
Next to these two repositories we have a new
All in the same folder.
The two existing repositories need to be merged into a new monorepo
MonoRepo where we expect the following structure:
Modify old repository
First, we need to make some (local) changes to our old
RepoA we will be preparing a move to the subdirectory.
|Modify the old repository to prevent merge conflicts|
If we perform some preparation in the old repo(s), we will avoid getting merge conflicts in the resulting
If 2 the same file exist in
RepoB it will cause a merge conflict if we try to merge them both into the root of
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)
|2||We can only merge one branch of both
|3||Update with all remote changes|
|4||Create a new branch
|5||Create a subdirectory
|6||Extended globbing allows pattern matching|
|7||Move everything into
|8||Some files are not moved with the
|9||Commit the change to
It is important to use
Merge into new repository
Next we need to switch to the
MonoRepo where the actual merge is performed.
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)
|1||Switch to the MonoRepo directory|
|2||Add the directory
|3||Perform the actual merge with unrelated histories|
|4||Remove the remote
|5||Push the merge history to the origin remote of
(Optionally) remove the temporary branch
We can clean up the local branch in the old repository to finish everything up.
cd ../RepoA git checkout --force master git branch -D prepare_monorepo cd ../
Now repeat all the steps for RepoB!