Post navigation

Linux, tip, Tools

Moving SVN repo’s to GIT

We have a lot of legacy code in our SVN server, and over the last couple of weeks i’ve been migrating all the old repos to GIT.

Its hasn’t been overly straight forwards luckily I found some great tutorials online,

http://john.albin.net/git/convert-subversion-to-git

and also these have been really useful on try to figure out how to merge two GIT repositories

http://blog.caplin.com/2013/09/18/merging-two-git-repositories

http://jasonkarns.com/blog/merge-two-git-repositories-into-one

Eventually I turned John Albin’s method into a bash script to make the process fairly automated see the Gist below

#!/bin/sh

# based on http://john.albin.net/git/convert-subversion-to-git with a some tweeks
# (NB: its quick and dirty, has no error checking and only works on repos structured with a stdlayout i.e. a trunk folder)
# use:
#
# $:./svn-to-git.sh new-repo-name http://user@server/repo/

BARE_REPO_NAME="$1"
SVN_PATH="$2"

cd ~/

# Create a Bare Repository
git init --bare ~/$BARE_REPO_NAME.git
cd ~/$BARE_REPO_NAME.git
git symbolic-ref HEAD refs/heads/trunk

# Clone the Subversion repository using git-svn
cd ~/
git svn clone $SVN_PATH --no-metadata -A authors-transform.txt --stdlayout ~/temp

# Convert svn:ignore properties to .gitignore
cd ~/temp
git svn show-ignore > .gitignore
git add .gitignore
git commit -m 'Convert svn:ignore properties to .gitignore.'

# Push repository to a bare git repository
git remote add bare ~/$BARE_REPO_NAME.git
git config remote.bare.push 'refs/remotes/*:refs/heads/*'
git push bare

# Rename “trunk” branch to “master”
cd ~/$BARE_REPO_NAME.git
git branch -m trunk master
rm -rf ~/temp

# Clean up branches and tags
git for-each-ref --format='%(refname)' refs/heads/tags |
cut -d / -f 4 |
while read ref
do
  git tag "$ref" "refs/heads/tags/$ref";
  git branch -D "tags/$ref";
done

https://gist.github.com/geraintp/89f96d8ca6ec2beabe4a
 

About Geraint Palmer

I am a front-end developer and software engineer living in Staffordshire, England. I love latte, good restaurants, photography, watching movies and occasionally snowboarding ( except when cartwheeling head first down the slope ;)