Patrick Bougie — web developer / entrepreneur from Montréal, Canada. Founder of WorkSked — a web app to manage employee work schedules and time off.

Convert SVN to Git repository

I recently decided to consolidate all my source code repositories into Git. Here are the instructions I used to convert my Subversion repositories. For more information, check git-scm.com.

Create a working folder

Create an empty working folder. Location and name are unimportant. We’ll be using ~/temp for this article.

mkdir ~/temp
cd ~/temp

Clone repository from Subversion

Convert the repository from Subversion to Git. Make sure you create an authors-file and reference it in the following command.

git svn clone http://svn.example.com/<repository> -A ~/authors.txt -T trunk -t tags -b branches <repository>

Move into repository

cd <repository>

Check for ignored folders or files

Check the repository for svn:ignore properties.

git svn show-ignore

Sometimes we need to ignore the contents of a folder but we want to keep the folder itself in the repository. Make sure the folder is empty before proceeding.

touch path/to/folder/.gitkeep
git add path/to/folder/.gitkeep

Repeat the above steps for all empty folders.

git commit -m "Forcing empty folder(s) into repository."

If ignored folders or files are found.

git svn show-ignore > .gitignore
git add .gitignore
git commit -m "Converted svn:ignore properties to .gitignore."

Reset file permissions

Reset your file permissions to start clean.

find . -type f -exec chmod 644 {} \;

Commit the changes.

git add .
git commit -m "Reset file permissions."

Check for external modules

Check the repository for svn:externals properties.

git svn show-externals

If external references are found, add each external repository.

git submodule add git://github.com/pbougie/<repository>.git path/to/<repository>

If you need to check out an earlier commit for a submodule.

cd path/to/<submodule>
git checkout <hash>
cd ../../..
git add path/to/<submodule>

Commit the included submodule(s).

git commit -m "Converted svn:externals properties to .gitmodules."

Remove the trunk branch

Since we are not going back to Subversion, we can remove the trunk branch.

git branch -rd trunk

Move out of the repository

cd ..

Create a bare repository

Create a bare repository to copy to a remote server.

git clone --bare <repository> <repository>.git

Copy the repository to a remote server

Copy the bare repository to a remote server so your colleagues can access it as well.

scp -r <repository>.git username@example.com:/path/to/git/repositories

You can delete the temporary repositories on your local server. They are no longer needed.

Clone the repository to your development server

Create a working copy and test the cloning process to make sure everything behaves as expected.

cd ~/Sites
git clone ssh://username@example.com/path/to/git/repositories/<repository>.git

If your project includes submodules.

cd <repository>
git submodule init
git submodule update

You now have a fully converted Git repository. Enjoy all the benefits of using Git. Repeat for each repository.

Feel free to contact me if you require development services or have any questions / comments.