Cloning an SVN Repository into Git with all Tags and Branches

git-svn is awesome, but I recently realized that it does not automatically create all tags and branches in Git that are present in SVN. It really just pulls the all into remote branches and leaves them there. This is the full process I used to carry all branches and tags over from an SVN repository into a Git repository.

Install Git SVN (Ubuntu)

sudo apt-get install git-svn;

Pull the SVN Repository into Git

If svn repo has standard trunk, branches, and tags, use this command at the root above those directories

git svn clone -s http://somesvnrepo.com/somesvnrepo/ --username=user@example.com destinationdirname;

Script to create tags

This will retain tag messages from SVN.

#!/bin/sh
git for-each-ref --format="%(refname:short) %(objectname)" refs/remotes/tags |
while read tag ref;
do
    tag=`echo $tag | sed "s|tags/||g"`;
    comment="$(git log -1 --format=format:%B $ref)";
    git tag -a $tag -m "$comment" $ref;
    git branch -r -d "tags/$tag"
done;

Script to create branches (after running tags script)

#!/bin/sh
git for-each-ref --format="%(refname:short) %(objectname)" refs/remotes |
while read branch ref;
do
    git branch $branch $ref;
done;
git branch -d trunk;

Push to Git Repo

git remote add origin https://somegitrepo.com/somegitrepo;
git push -u origin --all;
git push -u origin --tags;