Split Git Repository

by on under Tutorial
1 minute read

This is a quick tutorial to split a GIT repository into two differents while keeping the history.

Let’s begin, for the tutorial we use the following repository structure :

repository.git/
├── folder1/
├── folder2/
└── folder3/           

We want to split like the following structure :

repository_1.git/
├── folder1/
└── folder3/           

repository_2.git/
└── folder2/           

Creation of repository_1

Initialisation of repository 1

$ git init --bare repository_1.git
$ git clone --no-hardlinks git server:repository.git repository_1

Remove folder2 from repository_1

$ cd repository_1/
$ git folter-branch --index-filter "git rm -r -f --cached --ignore-unmatch folder2" -prune-empty HEAD
$ git reset --hard
$ git gc --aggresive
$git prune

Update remote and push the changes

$ git remote rm origin
$ git remote add origin git@server:repository_1.git
$ git push origin master

Creation of repository_2

Initialisation of repository_2

$ git init --bare repository_2.git
$ git clone --no-hardlinks git server:repository.git repository_2

Filter and exclude other files

$ cd repository_2/
$ git filter-branch --subdirectory-filter folder2 HEAD -- --all
$ git reset --hard
$ git gc --aggressive
$ git prune

Update remote and push the changes

$ git remote rm origin
$ git remote add origin git@server:repository_2.git
$ git push origin master