Member-only story
Merging Git repositories without losing history
I’m the first to admit that merging two repositories isn’t something that you would do often. On the other hand, I’ve seen my fair share of situations where teams were keeping separate repositories for their front-end SPA application and the accompanying back-end APIs and running into issues. Even though keeping them separate may give a sense of isolation, in reality you end up coordinating teams across those repos whenever a big feature needs to introduced. And don’t get me started on the challenges getting those HTTP APIs aligned. So moving to a mono-repo isn’t that uncommon after all.
With a bit of Git magic, it’s quite easy to merge the commits of one repository into a specific folder of another without losing the Git history. Here’s a high-level overview.
- Install
git-filter-repo
using the instructions Elijah Newren prepared on his repo. - As the
git filter-repo
command is a destructive operation, clone the source repo to a safe location - Determine the directory at which the code from the source repo should end up
- Determine the prefix you want to use to add to all tags from the source repo
- Run
git filter-repo
- Add the modified working directory as a git remote to the target repository