Last Updated: February 25, 2016
·
232
· deanrather

Making a new repo out of a subfolder

If you want to take a subfolder of one git repository, and turn it in to it's own repository (perhaps to re-include it as a subrepo, and share it among different repo's), that's actually quite easy!

Just use the git filter-branch function like this:

git clone <original repo>
cd <new folder>
git checkout <desired branch>
git filter-branch --subdirectory-filter <relative path to subdir> -- --all
git remote rm origin
git remote add origin <new repo>
git push origin <branch name>

This will preserve the history of all the files in the folder, while deleting all other folders and their histories.

Many props to gbayer.com For the detailed instructions!