Wednesday, April 27, 2011

[General Dev]–Using Git with an SVN server

Git is a great version control system, one which has many benefits over Subversion. However, many companies may already have existing Subversion setups that they are reluctant to move away from. In our office we use VisualSVN server and the TeamCity continuous integration server, and while we could migrate fully to Git, I don’t want to make the jump just yet, as I’m the only Git convert here – for now.
Enter Git SVN. Git SVN allows you to use a local git repository with a remote subversion server. This means that you can initialize git and commit like crazy, and then when you are ready to share your changes with the rest of your team, you synchronize them with the Subversion server.
This is not without its pitfalls – they are two very different version control systems, so if you are doing complex things with them, you may run into some troubles. However where I work we have small teams, and many of my projects are done solely by me, so Git SVN works out great for us, as the main reason for pushing the code to the Subversion server is so that it’s backed up and easily retrieved in the future.
I’ve run into a few issues at times with setting this up, so I thought that I’d create a quick guide for how I am now doing it on new projects.
First of all, I create my subversion repository in VisualSVN.
I like to setup the standard SVN directory structure, and Git SVN can accommodate this.
I then open a git bash at the parent folder of where my project will go (or where it currently is – this still works if you’ve already created your solution/folders, as long as they are named the same, or you move the files).
run the command ‘git svn clone –s URL’ where URL is the path to your subversion server. I’ve found that this must be in all lowercase. The –s tag specifies that you are using the standard SVN directory structure – emit this if you didn’t create it when you setup your repository.
This command will then check out your subversion repository into a directory of the same name as the repository – it will either create this, or use an existing one if it has the same name.
Next, make sure your .gitignore file is setup in the directory and then cd into it with ‘cd REPOSITORY_NAME’. Git status will show you the current files ready to be checked in.
Then you should be able to do your first git commit
Now your local git repository is up to date – so you are doing version control, and you can go along like this checking in regularly, then when it’s time to send the commits to the server, use ‘git svn dcommit’
Then you are all up to date! You can also use ‘git svn fetch’ to to a svn update from the server if others on your team have committed.

No comments: