Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Create a new git repo out of only a sub-directory of an existing repo
# clone the original repo
git clone
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
# 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