Friday, May 20, 2011

Migrating a Subversion repository to Git...

For years now I've been using Subversion to maintain history on a lot of documents for me (my resume, my writings, various configuration files for mutt and offlineimap, etc...).

I love Git, though.

So I decided to migrate my Subversion repo to Git. And I wanted to keep all of my history. And I leaned heavily on John Albin's work to do it.

Step 1: Map Subversion users to Git users.

The key goal here is to keep the history intact, which includes the names used (though in this case it's always me, but it's the principle, right?). To do that I created a text file that maps the Subversion account to an email account.

svn log -q | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | sort -u > users.txt

Now edit this file, which looks like:

mcpierce = mcpierce

and change it like this:

mcpierce = Darryl L. Pierce

Step 2: Clone the Subversion repo using Git.

This step is the crucial one, since it's here that we'll take all of that history out of Subversion and put it into a brand new Git repository.

Well, WE won't. Git will.

git svn clone [my Subversion repository] -A users.txt ~/temp

This took a while since it's going to migrate that history into the new repository. But, when it completes, you should be able to go to the repository and see all of that history.

cd ~/temp
git log

commit 50f06a699973bcd954fb1e61dc50deafee6a085f
Author: Darryl L. Pierce
Date: Fri May 20 14:13:43 2011 +0000

Updated my email configurations for home and work.

offlineimap can pull both work and home email down.

esmtp can send both work and home email.

git-svn-id: 7533b618-34f6-46d8-b546-5fbeb33e39a2

commit 00fb86a7314ec754108e0931d59a30464dce8510
Author: Darryl L. Pierce
Date: Thu May 19 19:47:59 2011 +0000

and you should see all of the commit author's names properly mapped over.

Step 3: Create the new remote Git repo and push the content

This step I'm not going to describe in a lot of detail. Specifically, I'm not going to tell you how to create a remote repository and access it. Instead, that's an exercise for you.

But, one you've created that remote repo and can access it, simple go to your new local repo and add that remote one and push your changes:

cd ~/temp
git remote add home [the remote git repository]
git fetch home
git push home HEAD:master

Now your Subversion repository history is in that remote repository.