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…

GitHub source code editor

Call me stupid, but that’s one awesome feature of GitHub that I haven’t noticed until now – source code editor.  When you view a file in your repository, there is an ‘Edit’ button on the toolbar above it.  That provides you with a simplistic text editor (with controls for tab/space indentation and wrapping), as well as a commit log fields.  Here is how it looks:

It’s not something that you would use for the majority of the coding of course, but it is a really nice option for quick edits all around.  Especially, if you have non-technical people in the project who edit README and other documentation files, and have no idea about Git at all.

SmartGit — The Easy-to-Use Git+Hg+SVN Client

SmartGit — The Easy-to-Use Git+Hg+SVN Client

Personally, I prefer command line tools that allow me the greatest flexibility and control.  However there are many people who feel more comfortable in graphical environments.  For them, SmartGit looks like a good option.

SmartGit is an easy-to-use graphical user interface for Git, Mercurial and Subversion with optimized work-flows. SmartGit supports all Git and Mercurial features needed for every-day work in software development projects:

  • Local working tree operations
  • Status, diff, log
  • Push, pull, fetch (for all protocols)
  • Tag and branch management
  • Merge, cherry-pick, rebase, revert
  • Submodule support
  • Stash management
  • Remotes management
  • Advanced SVN support (use SmartGit as SVN client)