In order to make life easier for our development team, we recently undertook the task of merging all of our separate application repositories into one, all inclusive repo. Logically, the previous structure looked like:
What we wanted was this structure:
Ideally, we'd find some way to preserve the histories of the files on trunk. We made the decision not to preserve the branches. This was not a big deal. The nature of the multi-repo approach meant that all branches were useless, if anyone bothered to branch at all.
Unifying these repos while preserving the history of the files on the respective trunks was pretty straight forward with some
git magic and elbow grease.
Step 1: Create the Repo
Step 2: Import Existing Code
You'll need to do the following two steps for each of the repositories you want to combine into the monolithic one.
These commands add the existing repository as a remote for your monolithic repo, then fetch the changes into that repo. The merge command takes whats in the existing repo and puts it on the
master branch of monolithic repo.
While fetching the each additional remote, you will receive warnings about the lack of common commits. This is okay. The files do not have a shared history.
Step 3: Copy the merged files into the correct subdirectory
This is the bit that requires some elbow grease. When you merge the remote repo, the merged repo is going to have all the directories at the top level. Most likely you'll want these in a sub directory. I used
Finder to move the files I wanted.
After the files were in the correct subdirectory, I used the following git commands to properly add the changes so that the history won't be lost.
The important line is
git add -u which will mark all the removed files as renames when used in conjunction with
git add <subdir_name>. This is the trick to maintaining history.
You'll need to repeat steps 2 and 3 for each of the repos you want to merge. Once you've finished, you should have one complete monolithic repo. Happy branching!
Did you like this? Please share:
The Lost Year: A Failed Experiment to Switch Away From Mac
Fed up with the Apple Keyboard, I bought a ThinkPad, installed Linux, and promptly decided that I hated computers.
Maker's Space, Manager's Space
The Grand Remote Work Experiment: A Retrospective
The COVID-19 pandemic has lead to an unexpected experiment in remote working. What has worked and why?