How to migrate projects from Subversion to Git with svn2git 🦅
Tags: devops, git, code, tooling
Published: 2021-11-26 01:13:03

---
title: How to migrate projects from Subversion to Git with svn2git 🦅
published: true
date: 2021-11-04 23:15:00 UTC
tags: devops, git, code, tooling
canonical_url: https://www.benjaminrancourt.ca/how-to-migrate-projects-from-subversion-to-git-with-svn2git
cover_image: https://www.benjaminrancourt.ca/images/pexels-adiprayogo-liemena-7826494.jpg
---
Do you remember [Subversion](https://subversion.apache.org/) (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](https://git-scm.com/). 🤖

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](https://github.com/nirvdrum/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](https://www.manjaro.org/) as my Linux distribution and the [yay](https://github.com/Jguer/yay) tool to install dependencies from [AUR](https://aur.archlinux.org/) (_Arch User Repository_), I just have to type:

```bash
yay --sync ruby-svn2git

# or
yay -S ruby-svn2git
```

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:

```bash
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
```

#### 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>
```

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:

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

It should produce a list of usernames, like:

```
outb3145
ranb2002
```

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:

```bash
# 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}"
```

And finally, run the svn2git utility:

```bash
# 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'
```

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.

```bash
# 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
```

### 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:

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

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. 😉