GIT

Migrate from SVN to GIT in 7 easy steps

Are you stuck with Subversion (SVN) because of the commit history? Do you want to migrate to GIT and include all the history in the GIT repository? Read on ..

GIT is a fantastic source repository. It is extremely fast and very easy to maintain branches and manage the workflow. SVN was a pretty popular source control for a number of years but has fundamental flaws with the workflow making it. SVN is a centralized version control system. This means that Subversion allows you to store a record of the changes made to a project, but that history is stored on a central server. Unlike Git, which is distributed, you need to have constant access to an SVN repository to push changes. These changes are saved as the developer implements them. In addition, instead of having a copy of a project’s history on your local machine, you only have a copy of the code itself. In other words, to see how a project has evolved, you need to reference the central version of the codebase.

Since you are already reading this post, most likely you are already sold on GIT or are looking to migrate to GIT. We were in a similar situation last year. One of our code repository was on a hosted SVN server and we really wanted to move to GIT. However, the SVN repository had almost 10 years of commit history and we did not want to lose that history – which was the main resistance to moving to GIT. However, it is quite easy to migrate from SVN to GIT and retain all the commit history. We used the following steps:

NOTE: Most of these steps should be possible using a Linux or Mac terminal. We did this from a Windows machine. If you are using Windows, I would highly recommend getting Windows Subsystem for Linux (WSL): https://docs.microsoft.com/en-us/windows/wsl/install-win10

Step 1: Check all changes into SVN
Make sure everything is checked into the SVN repository. This would mean letting your team members know that they need to check in their changes too.

Step 2: Checkout the repository from SVN
mkdir /gitmigration
cd /gitmigration
svn co <SVN repository url>
Replace the <SVN repository url> with your hosted or local SVN repository. This will create a new folder and checkout the SVN repo into that folder

Step 3: Get all the authors that have committed their changes to the SVN repository over the course of time:
svn log -q | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | sort -u > authors-transform.txt
This will get all the authors and add them to a new text file authors-transform.txt

Step 4: Modify the authors’ list
The authors-transform.txt will give you a list of the authors – however, SVN will only give you the author usernames. GIT user’s format is slightly different – GIT expects the authors as Author Name <Email Address> format. So open the authors-transform.txt in any text editor and change the author names
e.g. my authors-transform.txt had the following entries
smokashi
user1
user2
and I changed it to
smokashi = Swanand Mokashi <[email protected]>
user1 = Developer One <[email protected]>
user2 = Developer Two <[email protected]>

Step 5 : Let’s migrate
mkdir /gitFolder
cd ..
git svn clone <SVN REPO URL> -T. --authors-file=gitmigration/authors-transform.txt --no-metadata --prefix "" gitFolder

Replace the <SVN REPO URL> with your SVN repository URL. At this point, you will have your git repository ready in the /gitFolder folder

Check if the history was migrated to GIT:
git log -n 3 --pretty=format:"%h - %an %ae %ar : %s"
This will give you a list of the last 3 commits, and you can see if the authors are correct for those commits

Step 6 : Branches
If you have any branches in SVN, we can move those over as well:
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
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
for p in $(git for-each-ref --format='%(refname:short)' | grep @); do git branch -D $p; done
git branch -d trunk

Step 7: Commit to GIT repository
Assuming you already have created a remote GIT repository on the host of your choice: GitHub, GitLab, Amazon CodeCommit, or Azure. Get the repo clone URL
git remote add origin '<GIT repo clone url>'
git push origin --all

Replace <GIT repo clone URL> with your GIT clone url.

And voila! you have migrated your SVN repository to GIT with all the history migrated as well.


Leave a Comment

Your email address will not be published. Required fields are marked *