Technologies, methods and tools

Split git repositories by subfolders with history

This article describes how to turn the subfolders in a git repository into new git repositories, keeping the history.

In our case we have a big git repository (2GB) with slides for a lot of courses. Every course was a subdirectory in the repository. So even if you hold just one course, you have to clone everything. That’s why we decided to put every course into a separate repository. We use SCM-Manager for our repository and could therefore use its REST api in the script.

Get the list of subdirectories. This could be done in several ways: Clone your repository and run a find/ls or use an api, if your git repository provides something like this. This is the first block in our script. If you like, you could run this as a separate step and change the directory list as you like.

For every subdirectory

clone the repo

run the filter

create a new repository with Access rights (owner and group write here)

push your changes to the new repository

clean up. After every git filter-branch run we have to clone the original repository again, because the filter changes your local repository.

NOTE: The sub directory name could contain spaces or even umlauts. So we have to replace that to get a valid repository name. sed is our choice here.