DEV Community

Cover image for How to migrate projects from Subversion to Git with svn2git 🦅
Benjamin Rancourt
Benjamin Rancourt

Posted on • Originally published at benjaminrancourt.ca on

How to migrate projects from Subversion to Git with svn2git 🦅

Do you remember Subversion (also known as SVN)? 🤔 Unfortunately, there is still a few teams that still use it, but ultimately want to migrate them to a better option: Git. 🤖

If you still hesitate to take some time to do this migration, rest assured, it doesn't take too much time with a simple tool named svn2git.

What is svn2git?

svn2git is a small tool for migrating projects from Subversion to Git. It is useful to use it if you want to keep the full history of the project. All commits, as well their authors, dates and comments, will be kept for all branches, the trunk (main branch) and tags.

How to install it?

Depending on your operating system and the tools you are using, the command to install svn2git may differ. In my case, as I use Manjaro as my Linux distribution and the yay tool to install dependencies from AUR (Arch User Repository), I just have to type:

yay --sync ruby-svn2git

# or
yay -S ruby-svn2git
Enter fullscreen mode Exit fullscreen mode

Voilà, it is now installed!

How to use it?

Preparation

Variables

To simplify the commands you are going to type, I suggest that first put together some information that you will need:

  • PROJECT_NAME : The name of your project, used to create a new empty folder, (like rabbit-cms)
  • SVN_PROJECT_URL : The URL of your old SVN repository (like svn+ssh://svn.benjaminrancourt.ca/projects)
  • GIT_PROJECT_URL : The URL of your new Git repository (it doesn't need to be created first) (like git@gitlab.com:ranb2002/rabbit-cms.git)

Once you have all the information, export them to your terminal so that you can use them as variables:

export PROJECT_NAME=rabbit-cms
export SVN_PROJECT_URL=svn+ssh://svn.benjaminrancourt.ca/projects/rabbit-cms
export GIT_PROJECT_URL=git@gitlab.com:ranb2002/rabbit-cms.git
Enter fullscreen mode Exit fullscreen mode

Authors file

Before using the tool, you will also need to prepare a file named authors.txt which will contains all the information about the authors that have committed in your project (username, full name and email address). The file looks like below:

ranb2002 = Benjamin Rancourt <noreply@BenjaminRancourt.ca>
(no author) = Unknown user <unknown@BenjaminRancourt.ca>
Enter fullscreen mode Exit fullscreen mode

If your project has a lot of collaborators, it may take a while to gather the necessary information. Fortunately, there is a command to list at least their usernames:

svn log "${SVN_PROJECT_URL}" --quiet | grep -E "r[0-9]+ \| .+ \|" | cut -d'|' -f2 | sed 's/ //g' | sort | uniq
Enter fullscreen mode Exit fullscreen mode

It should produce a list of usernames, like:

outb3145
ranb2002
Enter fullscreen mode Exit fullscreen mode

Use the list produced to complete your author's file (authors.txt) according to the previous

syntax. If you are unable to find the email address of a retired colleague (such as Joe Bloe), use a fictitious email such as Joe.Bloe@BenjaminRancourt.ca.

Export the SVN project

To make sure you don't commit unwanted files, I suggest you clone your SVN project into a new folder:

# Go to your Desktop directory, or elsewhere
cd ~/Desktop

# Make sure to copy the authors file here

# Create a new empty directory for your project
mkdir "${PROJECT_NAME}" && cd "${PROJECT_NAME}"
Enter fullscreen mode Exit fullscreen mode

And finally, run the svn2git utility:

# Print the beginning date
date '+%Y-%m-%d %H:%M:%S'

# Run the svn2git with the authors file defined previously
time svn2git "${SVN_PROJECT_URL}" --authors ../authors.txt 

# Print the finishing date
date '+%Y-%m-%d %H:%M:%S'
Enter fullscreen mode Exit fullscreen mode

As it can run for a while, I added the date commands and the time command to know how long it took. For some projects, it may take several hours, but it usually takes around fifteen minutes. ⏲️

Create the Git project

If there were no errors, you should now have a local Git repository for your project. 🎉 But, unless you are only working on your computer, you might want to push the code to your Git server.

# Add the Git repository as origin
git remote add origin "${GIT_PROJECT_URL}"

# Push all branches to the origin
git push --all origin

# Push all tags to the origin
git push --tags origin
Enter fullscreen mode Exit fullscreen mode

Clean up

If you look at the tags created in GitLab or GitHub, you may see some incorrect tags. 🤨 You are probably going to want to delete them.

For example, if you have tags that look like VERSION@REVISION (like 1.26@19330), use the following command to remove them:

# Delete the remote tags
git push -d origin $(git tag -l "*@*")

# Delete the local tags
git tag -d $(git tag -l "*@*")
Enter fullscreen mode Exit fullscreen mode

If you have other tags that you don't want, just adapt the regex used previously and run the new commands!

Conclusion

Hope this post helps you to get rid of Subversion once and for all. 😉

Top comments (0)