Using Git to split out code into a new repository

OK, so you have a Git repository which has lots of code in there and you want to split a chunk out into its own repository (perhaps to make it a library). It was a while ago now, but I think this is how I did it, thanks to this howto on GitHub. (Suggestions and corrections welcome!)

First, clone the full current repo:
cd
git clone git://myserver/original-repo
cd original-repo

Split out the current code you want into a separate repo – in my case, the directory “directory-I-want-to-split/” (this will remove everything else, leaving just this code in the master branch). Here I’m using the subdirectory filter, but there are a number of others:
git status
git filter-branch --prune-empty --subdirectory-filter directory-I-want-to-split/ master

Push this repo somewhere else, perhaps local (note, you need the file:/// directive):
mkdir ~/split-repo
cd ~/split-repo
git init
git pull file:///home/chris/original-repo

So now that we’ve split that code out into its own repo, let’s remove it from the existing repo.
cd ~/original-repo
git pull
git reset --hard
git filter-branch --force --tree-filter 'rm -rf directory-I-want-to-split' HEAD

Now clone this into a new, smaller repo (can do magic with unpacking I guess, but this seems easiest way to reduce the size after removing that code).
mkdir ~/new-repo
cd ~/new-repo
git clone file:///home/chris/original-repo

0 Responses to “Using Git to split out code into a new repository”


  • No Comments

Leave a Reply