Knowledge base

Repositories / Convert SVN to GIT


Note: This article currently has a high "know what you do" factor, and servers mostly as an outline of what to do and how to automate some of the tedious steps. If you have any questions, feel free to drop by on #curseforge and ping charon.

Converting a SVN repository to GIT

Note: You may need to use git-svn on Linux as there are reports that it is broken on Windows. YMMV. You can also try both the mingw port and a cygwin install and see if at least one works.

Do not switch your repository type to Git before you are satisfied with the conversion result, so that you can re-clone if something goes wrong!

  • Develop an author map file which contains the lines for each user who has submitted a commit to your SVN repository:
    svnuser = R. E. Alname <[email protected]>
    You can automate some of the task by running
    svn log svn:// | sed -ne 's/^r[^|]*| \([^ ]*\) |.*$/\1 = \1 <\[email protected]>/p' | sort -u > author-map
    which will scan the SVN history for all users, and make a dummy line for each. You can then look up their real names and emails as required.
  • Run the following command to import the SVN repository into a GIT repository:
    git svn clone -A author-map --no-metadata -s svn:// project
  • Fix up the history until satisfied.
  • Change the repository on the web-page to GIT.
  • Run the following commands to add your GIT repository:
    git remote add origin [email protected]:game/project/mainline.git
    git push origin master

How to fix the history

Remove useless commits

Transition from old repository to new repository

  • Make a graft that removes the commits that you do not want. Grafts are "fake parents" and can be used to remove emtpy commits, glue together disconnected history and even rebuild (simple) SVN merges as "real" Git merges. See man gitrepository-layout for usage.

Remove the project name from commit notes

On the old repository, you had to preface all your commit notes with the project name. This will get rid of them all. It will also drop empty commits, and set all grafts from the previous step "in stone".

  • Run the command:
    git filter-branch --prune-empty --tag-name-filter cat --msg-filter 'perl -pe "s/^project:\s*//"' -- --all

Create a tag for the branches

  • Run the following command:
    git for-each-ref --format="%(refname)" refs/remotes/tags/ | while read tag; do GIT_COMMITTER_DATE="$(git log -1 --pretty=format:"%ad" "$tag")" GIT_COMMITTER_EMAIL="$(git log -1 --pretty=format:"%ce" "$tag")" GIT_COMMITTER_NAME="$(git log -1 --pretty=format:"%cn" "$tag")" git tag -m "$(git log -1 --pretty=format:"%s%n%b" "$tag")" ${tag#refs/remotes/tags/} "$tag"; done
    The net effect is to convert SVN "tags" to annotated Git tags, preserving the tagger identity and timestamp.

Converting with Ruby

Please see this page for information on how to do this with Ruby.