Last Updated: September 30, 2021
· hector

Exporting a folder to a separate Git repository

Ever wanted to split your codebase in multiple pieces, and keep all the commits?

This is easier than you may think thanks to Git's awesomeness, all you have to do is:

  • Clone the repository
  • Run git filter-branch
  • Push to the new remote, done!

The commands

In your shell, all that would be (notice the placeholders):

$ git clone <old-repo-remote> my-repo-folder
$ cd my-repo-folder
$ git filter-branch --subdirectory-filter <folder-to-export> -- --all
$ git remote rm origin
$ git remote add origin <new-repo-remote>
$ git push --tags origin master

One caveat: tags may not be correctly rewritten. In that case they will still be pointing to the wrong commits and you will notice you've got a weird branch tree. Just remove or recreate the tags and everything will be back to normal.