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

  1. mkdir NewExport

  2. cd NewExport

  3. git clone <repo>

  4. cd into the cloned repo

  5. git filter-branch --prune-empty --subdirectory-filter /path/to/the/directory master

  6. change the remote  to point to your new repository 
    git remote set-url origin git@github.com:USERNAME/OTHERREPOSITORY.git

  7. git push origin master

Method2: Slurp the directory into a new repo

  1. mkdir export-source

  2. cd export-source

  3. git clone <repo>

  4. cd into the cloned repo

  5. git-subtree split --prefix=path/to/directory/ --branch=new-branch-name

  6. go someplace and create a new directory with the name of the new repo  `mkdir new_repo_name`

  7. cd new_repo_name

  8. git init

  9. git pull /path/to/export-source/checkout new-branch-name

section: