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:
- Easier to manager
- Nice GUI tools
- More intuitive
- Darling of the web development community
- Easy to host projects on GitHub
- All instances of a repository contain the entire project history
- 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 http://example.com/mysvnproject -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.