Skip to content

Splitting a repository

wcheng edited this page Jan 16, 2013 · 1 revision

This page describes instructions on how to split a git repository while preserving the relevant history, tags, and branches. For example, it allows you to take a repository like:

XXX/
  AAA/
  BBB/
  CCC/

And create a new repository that looks like:

YYY/
  AAA/
  CCC/

To create the repository YYY:

  1. Clone your repository.
    $ git clone XXX YYY
    $ cd YYY
  2. Preserve the branches you want and remove the origin to avoid having references back to the old repository.
    $ for i in BRANCH1 BRANCH2 BRANCH3; do git branch -t $i origin/$i; done
    $ git remote rm origin
  3. Remove any unneeded tags. To remove all tags, use following command:
    $ git tag -l | xargs git tag -d
  4. Filter your repository using filter-branch. Use --tag-name-filter cat to rewrite your tags and --prune-empty to remove empty commits.
    If you only want to keep a single subdirectory (e.g. keep BBB), you can use:
    $ git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter BBB -- --all
    Alternatively, if you want to keep multiple directories (e.g. keep AAA and CCC), use:
    $ git filter-branch --tag-name-filter cat --prune-empty --index-filter 'git rm -r --cached --ignore-unmatch BBB' -- --all
  5. Run the following cleanup commands.
    $ 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
Clone this wiki locally