How to create a Git repository from a Subversion working copy

11 Jun 2009

Sometimes I want to add the files handled by one version control system into another. Typically I have checked out files from a read-only Subversion repository and want to add all files to a Git repository, so I can work with them locally.

The following command has worked for me, and perhaps you’ll find it useful. Just change to the directory where you have your Subversion working copy. Then create a Git repository in the same directory:

git init

The Git workspace will be in the same directory as the Subversion working copy. This works fine since they store their metadata in different directories (.git and .svn respectively).

Now, to add all files that are under Subversion control to the Git repository, execute:

svn ls -R | grep -v '/$' | xargs git add

The svn ls -R command lists all files and directories, the grep command removes the directory names from the list, and then all the file names are sent as parameters to the git add command. Of course, this only works if you have those commands installed. It should work fine under Linux, OS X or other Unix variants. I haven’t tested it with Windows ports of the same commands.

Now that the files have been added, just commit them:

git commit -m "Files from Subversion revision XXXX"

Of course, you could have cloned the Subversion repository with the git svn clone command to the begin with, but at least one person I know (me) isn’t always that foresighted.

blog comments powered by Disqus