Wednesday, March 04, 2015

How To: Migrate from svn to git

Source control is a vital tool to any developer, if you're not using some form of source control when doing development then you're missing out. Creating a zip file with a version number in it does not count. Amazingly I still come across people doing this.

Most of the projects I work on are using git but there are still a few projects that are kept in subversion. There's nothing wrong with svn, it's perfectly fine but git does make it convenient to work on different features locally.

In the past I've moved projects from svn over to git by simply doing svn export then adding the exported code base to git. The drawback here is that you lose all the commit history and tags/branches.

Fortunately it's possible to migrate a svn repository over to git whilst keeping all of the history and some of the developers at Atlassian have made a handy tool to make it even easier.

Naturally you'll need svn and git but you will also need git-svn, Java, and 'svn-migration-scripts.jar' (available from BitBucket).

I did the process on a Fedora Linux system (you should use a caps specific system) so the commands used on this page could differ. For full details of the process you can refer to Atlassian's Migrating Overview page.

To start off make sure you have git-svn:
sudo yum install git-svn
Then if you havn't already downloaded it, get the svn-migration-scripts.jar:
wget https://bitbucket.org/atlassian/svn-migration-scripts/downloads/svn-migration-scripts.jar
The first part of the migration is to map all the users that have done a commit to the svn repo, to their corresponding git user. To do this use the migration-scripts tool then manually edit the generated 'authors.txt' file:
java -jar svn-migration-scripts.jar authors https://mydomain.com/svn/repos/myproject/ > authors.txt
For every user that has done a commit to the project in the svn repo, map them to a name/email combination that is used by git. eg:
BGATES = Bill Gates bill.gates@microsoft.com
Once that's done it time to get git-svn to get all the commits from the svn repo:
git svn clone --stdlayout --username=SVN_USERNAME_HERE --authors-file=authors.txt https://mydomain.com/svn/repos/myproject/ myproject
Change in to the newly created directory that the code has been cloned to. It's worth noting that git can be configured to a particular user both globally and for a particular repository so you may want to set your name/email for the repository before continuing:
git config user.name "name"
git config user.email "name@domain.com"
Now to convert all the svn tags to tags in git (assuming you have the standard svn layout of tags, branches, and trunk):
java -Dfile.encoding=utf-8 -jar ../svn-migration-scripts.jar clean-git --force
That's just about it. Assuming you already have an ssh key and a remote git server to push to then you can add the remote is the usual way and push the code. Note that by default git won't push the tags up to the server so use '--tags' or '--all' (not both):
git push -u origin master --all

No comments: