Splitting a git repository

Sometimes when you are starting with version control for your code, you dont know how big your repository is going to become. The decision whether to create a new repository for every module or to keep them all in one repository may be a difficult one.
When the modules are smaller, you would like to keep them all in one place. You can decide to move them in their own repository later. It took me some googling around to figure out how to split a git repo. This is fairly easy and this great blog post summarizes it very well. In this post, I am putting the lessons I learnt while splitting a git repo.

Please ensure that all your commits to the module are merged to master branch. The version logs of only the master branch would be preserved post the split.

Say, I have a git project called webservice.git. And it has the subdirectories service1/, service2/, service3/. Of these service1/ has grown large enough to be moved into its own repository.

Follow the following steps to move the folder service1/ into its own git repo, along with the logs of the master branch.

Step 1: Clone existing repo as desired repo:

$git clone --no-hardlinks git@gitserver:webservice.git service1

Step 2: Filter the branch and reset to exclude other files, so they can be pruned:

cd service1
$git filter-branch —subdirectory-filter service1 HEAD — —all
$git reset —hard
$git gc —aggressive
$git prune

Step 3: Create new empty repo on git server

Step 4: On the local machine, replace remote origin to point to new repo:

cd service1
$git remote rm origin
$git remote add origin git@gitserver:service1.git
$git push origin master

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s