Video in progress

Staff 🌿 Git

How do you move 15 service repos into a single monorepo without losing history?

I move each repo's history into its own folder with git filter-repo --to-subdirectory-filter, then merge it into the monorepo with --allow-unrelated-histories. The Git part is easy; the main work is the plan for moving the build, CI, ownership and open PRs.

📖 Answer

The technical part for each repo:

git clone --no-local https://git.example.com/order-service.git tmp-order
cd tmp-order
git filter-repo --to-subdirectory-filter services/order-service --tag-rename '':'order-service/'
cd ../monorepo
git remote add order ../tmp-order
git fetch order --tags
git merge --allow-unrelated-histories order/main -m "order-service monorepo'ga ko'chirildi"
git remote remove order

As a result, git log -- services/order-service and git blame show the old history. The tags are renamed with a prefix, otherwise 15 v1.0.0 tags collide.

The real complexity is outside Git:

  • Build. Merging 15 independent pom.xml files into one reactor or a Gradle composite build, and harmonizing dependency versions. The differences in Spring Boot and Jackson versions take a lot of time here;
  • CI. Build by affected modules, otherwise everyone complains on the very first day;
  • Open work. Open PRs do not move automatically: close them before the move or reopen them afterwards;
  • Ownership. CODEOWNERS should reflect the old repo permissions;
  • The old repos become a read-only archive, with the new address in the README.

I do not move everything at once: first 2-3 tightly coupled services, the tooling gets tested, then waves. During the move window, pushes to that repo are frozen, otherwise the last commits get lost.