Use Case: You have multiple related repositories that have existed for a long time that you would like to combine into a single monorepo, without losing the long informational git history of each one of those repositories.
Decide on the structure of your monorepoSection titled: Decide on the structure of your monorepo
You can of course organize your monorepo in any way you wish. The standard way is to have a packages
directory and sometimes an apps
directory (I think distinguishing those two different kinds of packages is nice).
However you decide, we need to initialize the monorepo with these directories. If you have an existing monorepo you’re adding to, you can skip this step.
Then for each repo you want to move:Section titled: Then for each repo you want to move:
1. Move all files except for .git into a subdirectorySection titled: 1. Move all files except for .git into a subdirectory
The goal is to move each file into the same directory structure that you want it to live in inside the monorepo.
So if I have a my-app
repo, I’m going to move every file (except for the .git directory) into my-app/apps/my-app
because I want its final resting place to be monorepo/apps/my-app
.
There’s nothing wrong with just moving these fils with your OS file explorer; that is the simpler approach and works just as well. But here are commands to do it with the terminal:
Then you’ll need to commit that change:
2. Merge the existing repos history into the monorepoSection titled: 2. Merge the existing repos history into the monorepo
You’re totally done with this repo now, so you can remove the remote you added earlier:
Now just repeat steps 1 and 2 for every repo you’d like to move over!
Acknowledgements
This post is largely a repackaging of a Medium article to add clarity and prevent it from being hidden behind a paywall. Big thank you to Chris Thompson for writing that.