deCODE - Our Newsletter for Jan 2022 is available for Download. 🗞   🥳
  Signup/Sign In

SVN to Git Migration

SVN is a popular centralized version control system. Sometimes, we may want to migrate our SVN repository to Git.

For this, we have a dedicated Git SVN Clone command that can be used to do this.

In this tutorial, we will learn how to migrate from SVN to Git.

SVN to Git Migration

Migrating from SVN to Git

Step 1

In SVN, a commit will contain only the user name, but Git commits can provide the first and last name of the person who made that commit.

Git also includes the email address of the committer. We first need to get the committer names from SVN and we then need to add additional details to make sure that Git commits can show us that information.

Run the following command to get the list of all committers in an authors.txt file.

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

The content of this file will look like the following:

jreacher = jreacher <jreacher>

Open this file and match the author names to their full name and email address.

jreacher = Joe Reacher <joe.reacher@gmail.com>

Step 2

Next, we will clone the SVN repository by using the git svn clone command.

We will pass the modified committer names file to make sure that Git commits have the information in the correct format.

$ git svn clone <SVN-repo-URL> --no-metadata --authors-file "authors.txt" --stdlayout c:\my_git_repo

The above command will create a Git Repository in the c:\my_git_repo folder.

The cloning from SVN to Git is completed in this step and you can skip the remaining steps if you do not wish to format the history of the project.

Step 3

SVN has an svn:ignore and it is a good idea to create a corresponding .gitignore file. Run the following command to do so.

$ git svn show-ignore > .gitignore

Commit the changes to the new .gitignore file.

Step 4

Now, we will also move the tags of the SVN repository to the Git repository.

The following command will do this.

$ for t in $(git for-each-ref --format='%(refname:short)' refs/remotes/tags); do git tag ${t/tags\//} $t && git branch -D -r $t; done

Step 5

Next, we need to properly migrate branches from SVN to Git. Run the following command to do this.

$ for b in $(git for-each-ref --format='%(refname:short)' refs/remotes); do git branch $b refs/remotes/$b && git branch -D -r $b; done

SVN also uses an additional branch called trunk but we already have a corresponding master branch and so we will delete the trunk branch.

$ git branch -d trunk

This step completes our migration.

We may also want to push this local repository to a remote server like GitHub. Create a new remote connection using the Git Remote command, then push the master branch to the remote repository using Git Push command.

$ git remote add origin <remote-repo-URL>
$ git push --set-upstream origin master

Summary

In this tutorial, we learned how to migrate from SVN to Git. If we do not care about formatting the commit history then we can directly run the git svn clone command with the SVN repository URL.

However, it is a good idea to make some changes so that we can easily understand the history of the project in Git.