Skip to content

Instantly share code, notes, and snippets.

@desseim
Created January 7, 2015 20:58
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 desseim/914e0663bee3f20d6ca0 to your computer and use it in GitHub Desktop.
Save desseim/914e0663bee3f20d6ca0 to your computer and use it in GitHub Desktop.
Create a new git repo out of only a sub-directory of an existing repo
# clone the original repo
git clone git@github.com:user/repo.git
cd ./repo
# filter all git history (including all branches) and only keep the commits affecting a given subdirectory
git filter-branch --subdirectory-filter the/sub/dir -- --all
# clean up git backups (only necessary to help making sure all that we don't want is gone afterwards):
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
git reflog expire --expire=now --all
git gc --prune=now
# we can now check that the history looks fine if we want to
# now push to a new repo:
git remote add new-repo git@github.com:user/new-repo.git
# push everything:
git push new-repo refs/remotes/origin/*:refs/heads/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment