Migrating from SVN to GIT with Branches and Tags

I needed to convert a number of small subversion repositories to git so that they could be hosted publicly on GitHub and some hosted on our private GitLab service. I am adding a few notes here of what I did to help my future self. If it helps anyone else – bonus.

Initialise

In an empty test directory initialise a git repository with the URL to your svn source:

git svn init http://mydomain.co.uk/svn/2DCODE_CPC --stdlayout --prefix=svn/

Check the settings are correct with:

git config --local --list

Authors

If you want to have valid git authors corresponding to your SVN users then create an authors file in the following format:

joebloggs =  Joseph Bloggs <j.bloggs@somedomain.fr>
johndoe = John Doe <joh.doe@anotherdomain.co.uk>

The word on the left of the equals sign is the svn username. On the right is the corresponding info to use within git. If you already have a copy of the svn repo locally you can run the following code to help generate a file of svn usernames as a starting point:

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

If you need to checkout the svn code you would first need to:

svn co http://mydomain.co.uk/svn/2DCODE_CPC

Get the Subversion Code

The next step was to fetch the code from the SVN repository (the URL was already initialised above with git svn init) and I have specified my authors-transform.txt file to give git identities to the svn usernames:

git svn fetch -A authors-transform.txt

At this point you should see all your code.  If no files come down consider removing the .git directory and run the ‘git svn init’ command again but omitting the –stdlayout option. I ran the following to have a look at all (-a) the local and remote branches:

git branch -a

 

git branchConverting Branches

There was shell scripts on some of the other articles I read to convert svn branches to git branches:

for branch in `git branch -r | grep "branches/" | sed 's/ branches\///'`; do
  git branch $branch refs/remotes/$branch
done

However that code did not work for me. I think it was related to the naming used in my SVN repo (see image above) as the word branch was not there. So I manually converted the branches as follows:

git branch BRYCE remotes/svn/BRYCE
git branch CPC remotes/svn/CPC
git branch EPSRC remotes/svn/EPSRC
git branch FABRICE remotes/svn/FABRICE
git branch original remotes/svn/original

I read that as, create a local git branch named BRYCE using the code from the remote SVN branch at remotes/svn/BRYCE.

Converting Tags

Since the command git branch -r was showing the SVN tags with ‘tag’ in the name I was able to use the following shell to convert:

for tag in `git branch -r | grep "tags/" | sed 's/ tags\///'`; do
git tag -a -m"Converting SVN tags" $tag refs/remotes/$tag
done

Pushing to GitLab or Github

After creating a new project on Gitlab/Github to house your code use the address to add a new remote origin:

git remote add origin git@someserver.com:jonny/2dcode_cpc.git

Then push the code up to that git remote repository:

git push --all
git push --tags

Browsing to the web interface I was able to see the code complete with the branches and tags:

GitLab Branch References:

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>