Skip to content

Instantly share code, notes, and snippets.

@inactivist
Created December 5, 2012 21:48
Show Gist options
  • Save inactivist/4219799 to your computer and use it in GitHub Desktop.
Save inactivist/4219799 to your computer and use it in GitHub Desktop.
Move a subdir from an existing repository into a new repository.
#!/bin/bash
# Extract a subproject's master branch from a specified repo into a new
# repository in the target directory, preserving history.
# usage: git-subdir-newrepo.sh path/to/repo newrepo_path subproject_path
# Based on stuff I found here:
# http://airbladesoftware.com/notes/moving-a-subdirectory-into-a-separate-git-repository
EXPECTED_ARGS=3
E_BADARGS=65
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Clone a subproject from a source repository to a new repo."
echo "Usage: `basename $0` path/to/repo new-repo-path repo-relative-subproject-path"
exit $E_BADARGS
fi
# First we clone the source repo to a new working repo.
git clone --no-hardlinks -b master $1 $2
# Next we discard everything other than the subdirectory we want, $3, promoting it to the root level.
cd $2
git checkout master
git filter-branch --subdirectory-filter $3 HEAD -- --all
git reset --hard
# Now we get rid of the objects we are no longer interested in.
git gc --aggressive
git prune
# Now let's get rid of the origin repo.
git remote rm origin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment