Tuesday, April 13, 2010

Pulling a Subversion Repository Into Git Locally

Over the past month, I have really enjoyed using Git with Subversion. However, when I was thinking about how I got Git all setup to access Subversion, I realized that I can't remember exactly how I did it. So in the interest of remembering how to get Git setup to work with Subversion, I am writing this post for myself and anyone else who is interested in using Git in a Subversion environment.

After installing Git, you are going to want to determine the URL of the Subversion repository that you are going to want to integrate with. For our purposes, let's use the Open Source project AppFuse, whose Subversion repository is hosted at https://appfuse.dev.java.net/svn/appfuse/trunk.

First thing to do is to clone the repository to your machine. This process of cloning is what is going to convert the Subversion repository into a local Git one. At this point, I am not interested in cloning the entire repository (all of the branches, tags). I am also not interested in cloning all of the revisions (at the time of this blog post, their Subversion repository is on revision 3333). Instead, I only want to clone the trunk with the past 100 revisions. Here is the command necessary to do just that:


git svn clone https://appfuse.dev.java.net/svn/appfuse -r3330:HEAD --prefix=svn/ -T trunk -b branches -t tags appfuse.git

(You can choose to omit the -T, -b, and -t options, if you so desire. We can always add those details later.)

If you are prompted for a user id and password, use the account 'guest' without a password.

During the cloning process you are going to notice that git is actually mapping the 100 commits in Subversion to a Git commit object in your local repository. As you might imagine, this process could take a very long time, depending on how many revisions you are cloning into your local repository.

After your clone has completed, you should have a directory called appfuse.git. Move to that directory and type the following

git log -3 --stat


This will show the last three revisions that have been made to this source code. The first commit looks like this:

Author: mraible 
Date:   Sat Feb 27 15:54:03 2010 +0000

    Turn off EhCache update checking.
    
    git-svn-id: https://appfuse.dev.java.net/svn/appfuse/trunk@3333 2aeb74e6-0f1c-0410-991c-92d95bb9df37

 web/common/src/main/resources/ehcache.xml |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
The most interesting thing about this log is the fact that you can tell what Subversion revision it came from by the line that begins with git-svn-id. You can see that it comes from the revision 3333. There are many other features of git log that I am not going to talk about, but do encourage you to look into.

Congratulations, you have successfully pulled the Subversion repository into Git locally. In the next couple of blog posts, I will spend some time discussing how to commit changes back to the repository, as well as how to track and commit to branches in Subversion.

Happy Coding.

No comments: