Anthony McLin

How to Migrate from SVN to Git on OSX

As I'm moving from Snow Leopard to Lion, I'm also in the process of updating my development environment, including MAMP, SVN, Eclipse, and more. I've decided to switch my versioning control from SVN to Git, mainly because Git is easier to work with, especially when it comes to merging, and I've had a lot of messy problems when working with SVN. Read more to see how easy it is to migrate.



Reasons for using Git:

  1. Easier to manager
  2. Nice GUI tools
  3. More intuitive
  4. Darling of the web development community
  5. Easy to host projects on GitHub
  6. All instances of a repository contain the entire project history
  7. Easy to move an entire repository to a new server or location

Since I'm moving to Git, how do I preserve my history from SVN? I don't want to lose that info and start over. Nor do I want to have to keep SVN running so that I can get to older stuff. So, on my Mac, how to go about migrating from SVN to Git while maintaing this info? Well, the first step is to make sure you have Git installed.

Prerequisites: X11, XCode. If you've been running your own SVN repository on a Mac, you probably already have these both installed. If not, they're on the System disc that came with your Mac.

Step 1: Update MacPorts

If you already have MacPorts installed, then update it. From a Terminal window:

sudo port selfupdate

If you don't have MacPorts, Get it.

Step 2: Install Git

There are downloadable binary packages available, but this is easier from MacPorts. From a Terminal window:

sudo port install git-core +svn

Notice, the switch +svn. Git-core as bundled by MacPorts doesn't include the svn hooks we're going to need.

You may run into an error during the install process about not having the Java Developers Kit (JDK). If so, you can download it from Apple (free developer registration required). Once installed, run the above install command again to complete the installation.

Step 3: Extract the SVN repository

From the terminal, navigate to where you'd like to keep your Git repositories. I like /Users/myusername/git/

Extract the entire SVN repository with the following command:

git svn clone -T trunk -b branches -t tags

This will pull your entire SVN repository, including the Trunk, Branches, and any Tags (releases) you have made. (You did follow best practices for SVN and separate your trunk from the branches and releases right?). Unless your project is extremely basic, this will take some time. For example, I pulled a Joomla website project with about 150 commits and it took about 20-30 minutes to complete.

When this process is complete, you'll see a new folder that matches your project name. At this point, it is a complete Git repository. If you open the folder, you'll see the trunk of your project, and the details and necessary Git database will be stored within a subfolder called .git

Step 4: Cleanup and Break from SVN

At this point, your Git repository is still linked to the SVN repository. I don't recommend you continue in this method as it's messy and ripe for problems. If you do want to keep in synch with your old SVN repository, you might want to read up on the limitations and best practices.

Download and install GitX. This wonderful GUI utility for OSX makes it easy to clean up, review, and manage your Git repo. From within GitX, open up the repository you just created. You'll see that in the Remote section, each of your SVN Tags will show as a separate branch, but they won't show as Git Tags. Simply right-click on each one and choose "Create Tag" to make a matching Git Tag for each one. Once complete, you're going to jump back to the terminal.

git clone OldRepositoryName NewRepositoryName

This will clone your Git repository, dropping all the messy remote links to SVN. Once this command is completed, you can verify it by opening this new repository with GitX. If everything meets your satisfaction, you can dump the temporary Git repository we created, and turn off your SVN server.



This doesn't work for me at all. I have a MacBook Pro running the most recent version of Mac OS X (Lion) (10.7.3). git is pre-installed. The first git command (to clone the svn repository) fails.

- Paul Johnson (not verified)

- Anthony McLin

Of course, I should have said that git is installed when Xcode 4 is installed on Lion.

Thanks for the tip to use MacPorts to install git. That's necessary to make your method work.

What about adding author information to the git repository? Your method doesn't include that but elsewhere I have seen a recipe for adding authors.

- Paul Johnson (not verified)

Add new comment