Following on from last weeks blog post, I’m still going through the process of moving my SVN repos over to Git.  Whilst last weeks method was a straight dump of files and losing all the revision history, this morning I’ve been attempting to actually convert the svn repo so that all the revision history is maintained.

This post provides a step by step guide on how I succeeded in converting an SVN repo to a Git repo. Whilst the requirements and commands worked well for me, if you’re on an OS other than Windows you might need to find your OS equivalents.

My scenario involves converting a remote SVN repo to Git, and then pushing it to a new remote Git repo.

Requirements

  1. SlikSVN -I used the latest version from here
  2. Git – I used the latest version from here | Alternatives here

Instructions

  1. If your SVN repo is on your local machine then skip to step #5
  2. The first step was to create a dump of my remote SVN repo.  This was quite straightforward as I use XP-Dev who provide a feature to export repos.  If you don’t have an easy to use interface you can try rsvndump
  3. On your local machine create a new svn repo to load your dump into.  You can use Tortoise SVN, or the following command:
    svnadmin create your-new-repo-name
  4. From this step onwards, make sure you’re using Git Bash and not Command Prompt (unless you’ve got Git configured to work in Command Prompt)
    Use the following command to load your dump file into your new repo:

    svnadmin load your-new-repo-name < your-dump-file.dump
  5. Before you start loading your SVN repo into Git, you’ll need to map your SVN users to into the Git author format.
    To do this just create a new text file called authors.txt and enter all the svn users who’ve accessed that repo in the following format:

    svnusername = Git Username <gitemailaddress>

    e.g.

    ashraf.vali = Ashraf Vali <[email protected]>

    Where ‘ashraf.vali’ is the svn user, and the rest is what my Git credentials will be.

  6. Now that you’ve got everything you need, you can run the Git command which will import your SVN repo into the Git format:
    At this point I would recommend going to #3 of the troubleshooting section below and following the steps to install the svnserve service, but you can try running this command first to see if it works without.

    git svn clone file:///pathto/your-new-repo-name new-git-repo-name –no-metadata -A authors.txt -t tags -b branches -T trunk

You should now have your SVN repo successfully converted into a Git repo.  If you encounter any username errors, just modify your authors.txt file to make sure you include the missing usernames.

Uploading The New Git Repo To A Remote Repo

Now that you’ve got a local Git repo, you can move onto uploading it all to a remote repository

  1.  Move into your newly create Git repo using the cd command:
    cd new-git-repo-name
  2. Set the remote origin of your Git repo to the remote repo URL
    git remote add origin ssh://your-remote-repo-url
  3. Push everything to the remote repo
    git push --all

Everything from your SVN repo should now be in your remote Git repo, including the entire revision history!

Troubleshooting

Here’s a few problems that you might come across, and their solutions

  1. Initialized empty Git repository in X:/new-git-repo-name/.git/
    E: ‘trunk’ is not a complete URL  and a separate URL is not specifiedTo fix this make sure you put ‘file:///’ before the full path to your SVN repo
    In my case it was:

    git svn clone file:///F:/Websites/Repos/tutelum tutelum-git –no-metadata -A authors.txt -t tags -b branches -T trunk
  2. Initialized empty Git repository in X:/new-git-repo-name/.git/
    Couldn’t open a repository: Unable to open an ra_local session to URL: Unable to open repository ‘file:///X:your-new-repo-name’ at C:\Program Files (x86)\Git/libexec/git-core/git-svn line 1941This will happen if you use backslashes (\) instead of forward slashes (/) in the git svn clone command i.e.

    git svn clone file:///F:\Websites\Repos\tutelum tutelum-git –no-metadata -A authors.txt -t tags -b branches -T trunk

    instead of (correct version):

    git svn clone file:///F:/Websites/Repos/tutelum tutelum-git –no-metadata -A authors.txt -t tags -b branches -T trunk
  3. Couldn’t open a repository: Unable to open an ra_local session to URL: Unable to open repository ‘file:///X:/your-new-repo-name’: Expected FS format ‘2’; found format ‘4’ at C:\Program Files (x86)\Git/libexec/git-core/git-svn line 1941This happens when your SVN repo format is newer than the SVN format included in with Git.  In order to fix it you’ll need to
    install svnserve.exe as a Windows Service.

    To fix first run the following command BUT make sure the filepath to svnserve.exe, and path to the SVN root matches up to your computers’:
    The root should be the path TO, but NOT INCLUDING your-new-repo-name
    Use Command Prompt As An Adminstrator

    sc create svnserve binpath= "\"C:\Program Files\SlikSvn\bin\svnserve.exe\" --service -root F:\Websites\repos --listen-host 0.0.0.0" displayname= "Subversion Server" depend= tcpip start= auto

    Then start the service using:

    net start svnserve

    Finally, update your git svn clone command to use the SVN server instead of the filepath.  For me this was:

    git svn clone svn://localhost/tutelum tutelum-git –no-metadata -A authors.txt -t tags -b branches -T trunk

Final Words

Whilst I’ve tried to provide as much detail and be as thorough as possible, do bear in mind that I’m fairly new to Git and this was just the easiest way I found of doing it and that I may have done something wrong.  If you do know of a better method or want to suggest improvements to this guide feel free to post in the comments and I’ll update it accordingly.

Thanks!