Separating history of a git repository subtree
February 20th, 2010
Most people probably already know that it’s possible to merge histories of multiple repositories by using the subtree merge strategy [1]. However, sometimes you also need to separate/decouple the history of a repository’s subdirectory into a stand-alone repository. This post outlines how.
In this example we decouple dokuwiki/lib/plugins/piwik from the dokuwiki repository.
$ git clone --no-hardlinks ~/projects/others/dokuwiki dokuwiki-piwik $ cd dokuwiki-piwik $ git filter-branch --subdirectory-filter lib/plugins/piwik HEAD # should list about as many commits as there are in the subdirectory $ git status # On branch master # Your branch and 'origin/master' have diverged, # and have 18 and 62 different commit(s) each, respectively. # nothing to commit (working directory clean)
Now some cleanup to get rid of the main repository objects (assuming you want that):
$ git remote rm origin $ git show-ref # remove all the refs $ git tag -l # remove all the tags $ git gc --aggressive --prune=0
Also see stackoverflow.com for discussion about keeping branches and tags in the decoupled repository.