Skip to content

Instantly share code, notes, and snippets.

@calavera
Created January 5, 2016 16:52
Show Gist options
  • Save calavera/287289b7908982b16bf3 to your computer and use it in GitHub Desktop.
Save calavera/287289b7908982b16bf3 to your computer and use it in GitHub Desktop.

How to extract parts of a repo preserving history

Move the directories to extract to their new location (this step is optional)

$ git filter-branch --prune-empty --tree-filter \
    'if [ -d api/client/lib ]; then mv api/client/lib client; fi; if [ -d api/types ]; then mv api/types types; fi' \
    -- --all

The previous command moves api/client/lib to client and api/types to types, those are the directories that we'll extract to the new repository.

Remove other directories that we don't want in the new repository

git filter-branch -f --index-filter \
    'git rm --cached -qr -- . && git reset -q $GIT_COMMIT -- client types'
    --prune-empty -- --all

The previous command removes everything except the client and types directories.

Move the results to a new repository

  1. Create new repository with git init in a different directory.
  2. Add the directory you want to extract the directories from as a remote to the new repo with git remote add OLD_REPO ../OLD_REPO.
  3. Pull the branch where you extracted the directories to the new repo with git pull OLD_REPO BRANCH.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment