Migrate a Git Subfolder to a New Git Repository

I recently needed to move several packages from subfolders in a Git monorepo to individual repositories while retaining the history of each package. It turned out to be a relatively painless process.

Requires Git >= 1.7.11

  1. Split the original repo
cd </path/to/orig-repo>
git subtree split -P <name-of-folder> -b <name-of-new-branch>
  1. Pull into the new repo
mkdir ~/<new-repo>
cd ~/<new-repo>
git init
git pull </path/to/orig-repo> <name-of-new-branch>
  1. Done! From here you can proceed in whatever way fits your use case—add a remote, delete the original subfolder, etc.