Git : separating folder into different repository, with history

First things first.  If you don’t use git for version control yet, stop right now and go plan your migration.  You’ll thank me later.  Now.  A few days ago I had a tricky problem.  A chunk of code that was initially all over the project has been refactored into a pretty much separate library.  It was still a part of the same project, but in a folder of its own.

Then, a realization came that this library can be used from a few other projects.  A separate git repository in combination with ‘git submodule‘ would do a better job.  But just initializing a new repository and copying files seemed like a bad hack.  We’d much rather keep all the commit history, contributors and timestamps.  But is that even possible?

Turns out, it is.  And quite simple too.  Stack Overflow to the rescue.  I’ll copy the code here just in case it disappears.

git clone --no-hardlinks file:///SOURCE /tmp/blubb
cd blubb
git filter-branch --subdirectory-filter ./PATH_TO_EXTRACT  --prune-empty --tag-name-filter cat -- --all
git clone file:///tmp/blubb/ /tmp/blooh
cd /tmp/blooh
git reflog expire --expire=now --all
git repack -ad
git gc --prune=now

It worked like a charm! The above lines gave me a local git repository with just the necessary folder and all the relevant commits’ history. All I had to do after is add a remote repository and push the code to GitHub.

This is one of those perfect examples of how powerful git is.  It’s also an example of git usage, which I would have probably never figured out on my own…

Leave a Comment