Pither.com / Simon
Development, systems administration, parenting and business

Extracting a subdirectory from git as a new git repository

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.


On Nov. 10, 2010, 7:57 a.m. Robert said...


Too bad the branches the big-old-repo had dont follow... otherwise, simple and great

On June 15, 2011, 9:18 a.m. ssp said...

Your notes just saved me a lot of time and headache, thanks!

On Jan. 20, 2012, 11:06 a.m. Rob said...

Very useful. Thanks.

On March 14, 2012, 11:45 a.m. Wojtek said...

Great post, saved me a lot of time :-)

Add a comment