How to convert a Subversion repository to a Git repository

I needed to convert several Subversion repositories to Git. Most of my process for this comes from this post. The following are some condensed steps and additional notes.

  1. Work in a new no-metadata folder; this helps keep your working environment isolated, and will let you re-use the users.txt file if you need to do this to multiple repositories. Create the users.txt file and ensure all authors in the repo are listed. To get started, do something like this, where SVN_URL is the URL of the repo:

    svn log -q SVN_URL | awk -F " \\\\| " '/^r/ {print $2" = "$2" <>"}' | sort -u > users.txt
    

    Note that this will preserve spaces in user names, if present (some other snippets you might find are broken and will not). Now edit users.txt and fill in details. You might need to add a (no author) = No Author <no@author.invalid> line, too, if there are any commits which have no author, such as the empty ones created by some techniques that manipulate dump files to purge files.

  2. Use git-svn like this (replace SVN_URL with the correct URL) to clone the repo without metadata; add --stdlayout if using standard branch folders, or as many -T, -t, and/or -b options as necessary:

    git svn clone --no-metadata --authors-file=../users.txt SVN_URL
    
  3. cd into the new clone's folder.

  4. If necessary, change “master” branch to “main”: git branch -m main

  5. Purge history of bad commits, e.g., those by “(no author)”. Locate these manually, then perform an interactive rebase like so:

    git rebase -i [SHA just prior to first bad commit]
    

    Drop (d) the offending commits.

  6. Convert SVN “tags” branches to real git tags:

    for t in `git branch -r | grep 'tags/' | sed s_tags/__` ; do
        git tag $t tags/$t^
        git branch -d -r tags/$t
    done
    
  7. Now it should be about ready to push to the new Git host:

    git remote add origin GIT_URL
    git push -u origin main
    
  8. Then make a fresh clone of GIT_URL. Done.


Comment to add? Send me a message: <brendon@quantumfurball.net>

← Previous