Spinning Off a Git Repo

I’ve been hacking away at this one project for the last three-plus years. It started out with a limited scope, and it made sense to keep it buried in an existing git repo - it was closely related to the rest of the code base. Quickly the scope grew, and for the last two years I’ve known I should pull it out into its own repo. That’s a daunting project, so I kept putting it off.

Until today. This codebase is going to get some wider circulation, so it was finally time to take the plunge. Greg Bayer put together a fantastic guide for how to get this done: Moving Files from one Git Repository to Another, Preserving History. I’m capturing the commands here, just in case I ever need to do this again.

First, make a local clone of the starting repository, and filter out everything except the subdirectory of interest:

git clone <git repository A url>
cd <git repository A directory>
git remote rm origin
git filter-branch --subdirectory-filter <directory 1> -- --all

Then, clone the destination repository, and pull in the master branch of your stripped local clone of the starting repository:

git clone <git repository B url>
cd <git repository B directory>
git remote add repo-A-branch <git repository A directory>
git pull repo-A-branch master
git remote rm repo-A-branch

I probably would have tried to pull this off in my working copies of the two repositories, so Greg’s comments to start with new clones and then disconnect them from the remote were solid.