Move a SVN repository to Git with the whole commit history

It is in your mind, like a worm that eats away the apple, "I have to switch to Git". And one day it happens and you realize that it was not that diffcult.

I started using Git as my local repository, but still using SVN as the central repository with git itself thanks to the git svn set of commands. After some time I decided to entirely move the vast majority of projects from SVN to Git but of course not by creating a new fresh and empty repository but importing the whole svn commit history, as if the commits were made using Git itself ten years ago.

This is how you can do it:

Import SVN repo to Git: Transition guide

I did this on mac several times, but you can do it in other systems as well. First of all, open up a terminal, and install the fantastic tool svn2git. The one-liners are:

#Ubuntu only:
sudo apt-get install git-core git-svn ruby rubygems
# Others: svn2git installation:
sudo gem install svn2git --source

Then you have available the new svn2git command. Create a new directory:

mkdir mygitrepo
cd mygitrepo/

Depending on the structure of your SVN repositories you have to play with the options of svn2git, the README is pretty clear. If you don't want to import the whole trunk/ tags/ branches/ and pick a single branch or folder, no matter how you named it then you have to do something like this:

svn2git --rootistrunk --username yourusername

Ensure to launch this command inside the new folder! But wait! We forgot to map the SVN usernames to Git usernames.

If you intend to upload this git repo to an external service like Github or Bitbucket I would recommend using the exact usernames there are in those services. You need the file that says what SVN username corresponds with which Git usernames. The file you have to create is ~/.svn2git/authors and the syntax looks like this:

svnusername = gitusername

For instance, the users "albert" and "obokaman", etc... in my local SVN repo correspond to the following in GitHub:

albert = alombarte
obokaman = obokaman-com
root = alombarte
pablo = pablor

It is very important that you add all the authors that ever commited to your repository in this  ~/.svn2git/authors file. This is why I added root in the example. When this is complete, run the previous command.

Now you have a git repository with all the svn history. Maybe the next thing to do would be uploading the local Git repo to an external service like Github or Bitbucket, keep reading on the next article.