Skip to content

Instantly share code, notes, and snippets.

@underhilllabs
Created March 3, 2012 19:14
Show Gist options
  • Save underhilllabs/1967591 to your computer and use it in GitHub Desktop.
Save underhilllabs/1967591 to your computer and use it in GitHub Desktop.
Split a subtree of a git repository into a separate repo.
#########################################################
#
# classes is a git repository with many class projects
# I want to split one of those projects into its own repo
# and I want preserve the commit history
#
# The super top-level repo is classes.git
# Within classes.git is a sub-directory "networks/project2"
# we want to create a new repository with just the history of
# "networks/project2"
#
#########################################################
# the subtree
SUBDIR = "networks/project2"
# the top level git repository
SUPER_REPO = "~/projects/classes.git"
# new project name
NEW_REPO = "project2.git"
# first clone the super class repo, because we will be removing all other commits
# that are not related to subtree
git clone $SUPER_REPO $NEW_REPO
# cd into new repository clone
cd $NEW_REPO
# now filter-branch and remove all history unrelated to networks/project2
git filter-branch --prune-empty --subdirectory-filter $SUBDIR master
# now all that remains is commit history of sub-directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment