Skip to content

Instantly share code, notes, and snippets.

@danielpcox
Last active August 29, 2015 13:56
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 danielpcox/8790755 to your computer and use it in GitHub Desktop.
Save danielpcox/8790755 to your computer and use it in GitHub Desktop.
Extract subdirectory in git, then slim down the history
$ git clone blahblahblah # <-- fresh clone of the repo
$ du -sh .
768M # <-- holy macaroni, that's huge
$ git filter-branch --subdirectory-filter path/to/subdir --prune-empty --tag-name-filter cat -- --all # <-- Actual filter
...
### Now we'll delete all references to those unchanged refs
$ git reflog expire --expire=now --all # <-- expire the reflog refs
$ rm -rf .git/refs/original
$ vim .git/packed-refs # <-- open this file and delete anything you were warned about as unchanged above.
# notice that tag refs span two lines: delete them both.
$ git gc --aggressive --prune=now # <-- delete all irrelevant stuff
$ du -sh .
5.4M # <-- that's a lot smaller
# use git >= 1.8.5.3 !
# extract subdirectory and remove references to irrelevant history
git clone --mirror REPO
cd REPO.git
git filter-branch --subdirectory-filter PATH/TO/SUBDIR --prune-empty --tag-name-filter cat -- --all
rm -rf refs/original
rm -f packed-refs
# clean up
git reflog expire --expire=now --all
git gc --aggressive --prune=now
git reflog expire --all --expire-unreachable=0
git repack -A -d
git prune
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment