Merging git repositories without lose commits history

In this tutorial I’ll show how to merge git separated repositories in a new one without lose it’s commits history

First you need to create a new repository and start git

git init

Create a (you should have an initial commit)

dir >
git add .
git commit -m "Create"

Add your merge repository as remote

git remote add merge_repo <repo URL>
git fetch merge_repo

Merge your repository | * — allow-unrelated-histories it’s because since 2.9.0, Git doesn’t allow automatic merges with repositories with different histories

git merge merge_repo/master --allow-unrelated-histories

Create a new folder to your first repository

mkdir FirstRepoFolder

Now move all Repository files to your folder

git mv <files> ./FirstRepoFolder

After you move all of your files, you should commit

git commit -m "docs: move FirstRepo into a paste"

Now youve already complete your first merge

To merge the second repository, let’s reset the remote repository

git remote set-url merge_repo <repo URL>
git fetch merge_repo

And follow the same steps as the first

git merge merge_repo/master --allow-unrelated-histories
mkdir SecondRepoFolder
git mv <files> ./SecondRepoFolder
git commit -m "docs: move SeconddRepo into a paste"

And that’s all, now you have yours repositories merged without loose it’s commits history

*As you can see, you could repeat this process as many times as you want with others repositories.