Say your git repo consists of a subdirectory “app” and a subdirectory “database” (or “frontend” and “backend” or “team-a” and “team-b”) and you realize each directories content should be in its own repository. Here is how to split the repo.

To create a repo that consists of the content of the “database” dir and its history, execute

git filter-branch --prune-empty --tag-name-filter cat --subdirectory-filter database -- --all

and then push it to a new remote:

git remote rename origin old-origin
git remote add origin <new-remote>
git push -u origin --all
git push -u origin --tags

This is what Gitlab states on a newly created project page. However in git push -u origin –all “-all” should be replaced with “–mirror” because “-all” pushes all local branches only so you would need to checkout all branches you want to keep. “–mirror” pushes remote branches, too.

In the same fashion apply “filter-branches” to the “app” directory.