Skip to content

Instantly share code, notes, and snippets.

@spectra
Created October 1, 2015 19:23
Show Gist options
  • Save spectra/8bf5c767148c3c6e7f75 to your computer and use it in GitHub Desktop.
Save spectra/8bf5c767148c3c6e7f75 to your computer and use it in GitHub Desktop.
Extract a git directory as a separated project
#!/bin/bash
SUBDIR=$1
if [ "$SUBDIR" = "" ]; then
echo "You have to enter a subdir as first arg"
exit -1
fi
if [ ! -e $SUBDIR ]; then
echo "$SUBDIR must exist. Are you in the repo root?"
exit -1
fi
echo "Removing old origin"
git remote rm origin
echo "Filtering only the requested subdir ($SUBDIR)"
git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter $SUBDIR HEAD
echo "Purges repo of all unrelated references"
git reset --hard
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
git reflog expire --expire=now --all
git gc --aggressive --prune=now
echo "All done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment