Migrate SVN to Git

So you have a svn source and need to migrate to a new version control using git. Below are the neecessary steps to do it.

Prequiresite

This's done in CLI so you will need

  • svn client
  • git client
  • git-svn otherwise, you will get an error
    $ git svn -h
    git: 'svn' is not a git-command. See 'git --help'.
    Did you mean one of these?
          fsck
          show

Note

  • {source}: path to the svn folder (Eg: ~/Document/code/svn/project-a)
  • {target}: path to the git folder (Eg: ~/Document/code/git/project-a)
  • {svn-url}: url to the svn source (Eg: https://svn.my-organization.com/code/svn/project-a)
  • {git-url}: url to the svn source (Eg: https://git.my-organization.com/author/project-a)

Step 1: prepare the svn code base

svn update

Also, resolve any conflicts if necessary

Step 2: create authors file

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

These 2 command will create a new file authors-transform.txt at the {source} directory with the following format:

loginname = Joe User1 <user@example.com>
loginname2 = Joe User2 <user@example.com>

We will later serve this file to the git svn -A command. Read more about this argument in this documentation

Step 3: clone from svn

$ cd {target}
$ git svn clone {svn-url} --no-metadata -A {source}/authors-transform.txt .

These 2 commands will fetch, then clone with commit message, file changes, ....

Depend on how extensive your svn code base is. This process will take some time to complete.

$ git svn clone {svn-url} --no-metadata -A {source}/authors-transform.txt .
Initialized empty Git repository in {target}/.git/
	A	package-lock.json
	A	app/utils/index.js
	A	app/utils/Helper.js
	A	app/models/Test.js
	A	app/controllers/controller.js
	A	routes/index.js
	A	package.json
	A	config/index.js
	A	config/constant.js
r1 = e3b0c44298fc1c149afbf4c8996fb92427ae41e4 (refs/remotes/git-svn)
	M	app/controllers/ApiController.js
	M	app/utils/Helper.js
	M	app/models/Test.js
  ...
  ...
Checked out HEAD: {svn-url} r592
  • r1 is revision 1
  • r592 is last revision. If your last revision is bigger than 1000, then you make yourself some coffe

Note:

  • {svn-url}: url to the svn repository
  • {source}/authors-transform.txt: path to the generated file in step 2
  • .: there's a dot (.) at the end of the 2nd command if you want to clone into {target}. It's because we already cd to {target} at the 1st command.

Step 4: (Optional) create .gitignore file

$ cd {target}
 
# create .gitignore
$ git svn show-ignore > .gitignore 
 
# add .gitignore
$ git add .gitignore 
 
# commit .gitignore
$ git commit -m 'Convert svn:ignore properties to .gitignore.' 

Step 5: Add remote origin and then push

At this point, the local branch is master

$ git remote add origin {git-url}
$ git push --set-upstream origin master

From now on, you good to go. Learn more about git-svn, and its basic examples