Pither.com

by Simon Pither, freelance developer and systems administrator

Extracting a subdirectory from git as a new git repository

Posted by Simon Pither
Wed, 4 Feb 2009

Some time ago (about 2 years according to the file timestamps) I created a git repository to house all of my random personal files (letters, tax returns, random notes, etc).

Unfortunately at some point in the last two years I started to add in assorted other bits that really didn't belong, just because I needed somewhere to put them. To the extent that I now have at least two complete application code bases held in this repository!

Today I finally got around to extracting them out into their own repositories. Unfortunately taking just a subdirectory from within a git repository (complete with history) doesn't seem to be quite as easy as I'd hoped.

I decided that I wanted my final repository to be hosted by gitosis, which someone has expanded on a bit and is available in Debian.

Once gitosis was installed and I'd given myself write access to my new target repository, the process to extract the git subdirectory turned out to be...

$ cd big-old-repo
$ git branch to-extract
$ git filter-branch --subdirectory-filter path/to/extract to-extract
$ cd ..
$ mkdir tmp-new-repo
$ cd tmp-new-repo
$ git init
$ git pull ../big-old-repo to-extract
$ git remote add real-new-repo gitosis@my-server:real-new-repo.git
$ git push real-new-repo master
$ cd ..
$ git clone gitosis@my-server:real-new-repo.git

After all of that I now have a server hosted (and a local clone) repository that has it's top level at "path/to/extract" (and that's all it contains) from my old repository, complete with all history.

Post a Comment