From Multirepo to Monorepo: How to Merge Multiple Git Repositories into One
A guide on how to merge multiple repositories into one monorepo, making it easier to manage and maintain your codebase for small teams or individual projects.
You may have found yourself in a situation where you have multiple git repositories for different components of your project, and now you decided to switch to a monorepo approach.
Merging repositories while maintaining history
Here's a step-by-step guide on how to merge multiple repositories into one monorepo, while maintaining the commit history of each repository:
Let's assume you have three repositories:
- A backend repository, called "back"
- A frontend repository, called "front"
- A freshly created repository, called "monorepo"
- Clone the monorepo repository
- Install git-filter-repo
We will use git-filter-repo for manipulating the commit history of the repositories, which is a tool endorsed by the official Git documentation.
On MacOS, you can install it using Homebrew:
- Clone the repositories that you want to merge into the monorepo
These are throwaway clones that you are going to modify, so you should delete them later to make sure you don't accidentally push changes to the original repositories.
- Move the codebase of each original repository into a subdirectory
For this example, I want to have each repository in a separate subdirectory
of the monorepo. To do this, I will use the --to-subdirectory-filter
option
of git-filter-repo to move while preserving the commit history.
- Optional: Append a prefix to the commit messages of each repository
Appending a prefix to the commit message can help you identify the origin of each commit in the merged monorepo history.
- Add the repositories as remotes to the monorepo repository
Now, you can add the repositories as remotes to the monorepo repository.
- Merge the repositories into the monorepo
We will use git merge
to merge the repositories into the monorepo. This will
create a merge commit that will include the commit history of the original repositories.
- Push the changes to the monorepo repository
Finally, you can push the changes to the monorepo repository.
That's it! You have successfully merged multiple repositories into one monorepo.