To all those people who keep telling me that I am fat: look at you now, you frozen skinnies! Â It’s not even below zero, and all you can say is “OMG! It’s so cold!!”. Â Fat is awesome when it’s cold. Â Summer, on the other hand, is a completely different story…
Year: 2013
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…

