Exporting a subdirectory from a repo into a new repo
There are times when it is handy to export a subdirectory in repo into a new repo where you want to maintain the original git history of the files. Example: During the course of building a drupal site, a custom module is ready to become a contrib module, so it needs to be in a separate repository.
There are two methods
Method 1: Strip everything from a copy of the repo
-
mkdir NewExport
-
cd NewExport
-
git clone <repo>
-
cd into the cloned repo
-
git filter-branch --prune-empty --subdirectory-filter /path/to/the/directory master
-
change the remote to point to your new repository
git remote set-url origin git@github.com:USERNAME/OTHERREPOSITORY.git -
git push origin master
Method2: Slurp the directory into a new repo
-
mkdir export-source
-
cd export-source
-
git clone <repo>
-
cd into the cloned repo
-
git-subtree split --prefix=path/to/directory/ --branch=new-branch-name
-
go someplace and create a new directory with the name of the new repo `mkdir new_repo_name`
-
cd new_repo_name
-
git init
-
git pull /path/to/export-source/checkout new-branch-name