Converting an SVN repository to Git
Published on May 18th, 2009Author: Michele
Up to now I used Subversion (SVN) as control version system to manage my software projects. In the last months, due to specific necessities, it has been choosen to use Git as control version system for one project on which I’m working on. Git is a free and open source, distributed version control system, ideated by Linus Torvalds, designed to handle everything from small to very large projects with speed and efficiency. After a few weeks of test, I decided to use Git for another project that up to now was managed using SVN. Consequently, I had the need to understand how to convert an already existing SVN repository to a new Git repository, without loosing any information about operations that have been done on the project itself.
Converting an SVN repository to a Git repository is not a difficult operation. Following, I will describe the steps to do that.
Users File
As a first thing I created a new temporary directory called: “temp_prg”. Then I created a new text file called “authors” in which I defined a mapping between SVN user names to Git user names. This operations were done on a Mac machine using the command shell but…if you want you can do them using a graphic environment.
mkdir temp_prg cd temp_prg touch authors nano authors
The first comand (mkdir temp_prg) it has been used to create a new temporary directory called “temp_prg”. The second command (cd temp_prg) it has been used to enter into the “temp_prg” directory. The command (touch autorhs), instead, it has been used to create a new empty text file called “authors”. To edit this file I used the “nano” editor (see the last command). The content of the “authors” file should be something very similar to the following example. On the left side there are the SVN user names while on the right sides there are the user names of the new Git repository.
michele = Michele <michele_mail@testMail.org> christian = Christian <christian_mail@testMail.org> andrea = Andrea <andrea_mail@testMail.org>
Temporary Git Repository
At this point, to create a new temporary Git repository from the already existing SVN repository, enter the following command:
git svn clone --authors-file=/path_to/authors http://my_server/svn_repository/project
The "--authors-file" parameter is used to specify the file which contains the mapping between SVN user names and Git user names, in this example, the “authors” file. The last parameter (http://my_server/svn_repository/project) is the URL of the SVN repository. If the SVN repository has a standard “layout” with “trunk”, “branches” and “tags” directories, add the "--stdlayout" parameter to the previous command. If you want to convert the SVN repository skipping matadata about revisions, add the "--no-metadata" parameter to the previous command.
Repository Git Finale
At the end of the execution of the previous command, you will have a temporary Git repository on your local machine. If necessary, at this point you can set the correct permissions for the Git repository. Finally, enter the following command:
git clone --bare myproject myproject.git
where “myproject” is the name of the temporary Git repository while “myproject.git” is the name of the final Git repository. Check if the final Git repository (myproject.git) is ok. If everithing is ok, now you can delete the temporary Git repository. If you want you can upload the final Git repository to a remote server to have a shared repository. In this case, sometimes, you will have to set additional parameters. For example, you could do something like this:
cd myproject.git
git config core.sharedrepository 1
git config receive.denyNonFastforwards true
find objects -type d -exec chmod 02770 {} \;
Useful Links
Git official website: http://git-scm.com/
Git Documentation: http://git-scm.com/documentation
GitX, a GUI for OSX: http://gitx.frim.nl/
Leave a Reply