Skip to content

Instantly share code, notes, and snippets.

@uliwitness
Created April 9, 2012 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uliwitness/2343351 to your computer and use it in GitHub Desktop.
Save uliwitness/2343351 to your computer and use it in GitHub Desktop.
Shell script to extract a folder from a git repository to make a new repository with only the history for that folder.
# param 1 is path to root of repository to clone
# param 2 is folder to clone to. The folder's name must be the
# same as the folder to be extracted from the repository at param 1.
GIT='/Applications/Xcode.app/Contents/Developer/usr/bin/git'
$GIT clone --no-hardlinks "$1" "$2"
cd "$2"
$GIT filter-branch --subdirectory-filter "`basename $2`" HEAD #-- --all
# "-- --all" copies along tags and branches
$GIT reset --hard
$GIT gc --aggressive
$GIT prune
$GIT remote rm origin
$GIT update-ref -d refs/original/refs/heads/master
$GIT reflog expire --expire=now --all
$GIT repack -ad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment