I was faced with a (pretty common) scenario recently: I had been using one giant SVN repository for all of my various projects, and I realized it was time to step into the 21st century and get those projects into separate git repositories. I’ll admit, I was originally hesitant to use git; I thought SVN would be all I ever needed. However, I had to learn it due to my work with Drupal, and after a bit of forced exposure I realized just how nice it was to have the entire repository right there on my computer.
I did a lot of research, and tried a bunch of different ways to convert an SVN repository to multiple git repositories. Here’s what I came up with as the easiest way (this tutorial assumes you have git installed and are familiar with the command line). Note that if your SVN has gone through any major re-organizations, this probably won’t preserve the full history, and you’re probably off trying something more complicated. This process also works best with simple SVN setups that don’t have branches (but should work with branches as well).
Step 1: Setup authors-transform.txt
In order to translate the SVN usernames to meaningful git commit/author name+email addresses, we will make a text file that maps the former to the latter. If it’s a personal repo, you probably know the few usernames that had access, and can thus create the file from memory. If not, you might want to use a script to grab all users from the repo. Either way, you want a text file that looks like this:
[old SVN username] = [New Name] <[email@example.com]>
Tommy = Tommy Goode
Scott = Scott Mann
Step 2: Clone a project from the SVN repository into git
This step will be repeated once for each sub-project in the repo. Get yourself into an empty (except for authors-transform.txt) folder and run the following command:
[crayon lang=”sh”]git svn clone [base svn url] –no-metadata -A authors-transform.txt –trunk=[relative/path/to/project/trunk][/crayon]
If you have branches, add
--branches=[relative/path/to/project/branches]. Git will do it’s thing, and then BOOM, you have a git repo with all of your SVN commits. Do a simple [crayon lang=”bash”]git branch -rd trunk[/crayon] to get rid of the remote branch reference. You may also want to pack the repo, since as a brand new git repository it has not yet been optimized. [crayon lang=”bash”]git repack -d[/crayon]
Hooray! No more SVN. If you want to push it to a remote repo, just do a
[crayon lang=”bash”]git remote add origin [remote url]
git push -u[/crayon]