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
mkdir monolithic_repo
cd monolithic_repo
git init .
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.
git remote add <remote_name>
git fetch <remote_name>
git merge <remote_name>/master
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.
git add <subdir_name>
git add -u
git commit -m "adding <subdir_name>"
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.
All done
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!