Blog- Main Page - Books - Links - Resources - About - Contact Me

Subversion Tutorial Getting Started with Subversion on Windows

by Jared Richardson
http://www.jaredrichardson.net


What will we cover?

Downloading Subversion
Installing Subversion
Adding a new repository
Start a subversion server
Adding users
Get a copy of the code
See your Changes
Put your changes back in the server
Get everyone else's changes
Adding a new file



Downloading Subversion

You need to download the Windows installer first, but that's not quite as easy as you might think.  The Subversion download pages have source file packages, not installers. Source is great if you want to learn how to build Subversion, but if you just want to use it, you're out of luck.

To find the Windows installers, visit the 
Subversion home page,  the instead of following the Downloads links, following the Documents & files link. On that page, click the Releases link, then finally, the Windows link This will show you the Windows installers. I'm really not sure why the installers don't have a link from the Downloads area.

So follow this path to find the Windows installers.

Subversion Home Page --> Documents and files --> Releases --> Windows

The current release is named svn-1.2.3-setup.exe

Installing

I'm not going to discuss the install. It's a click through installation wizard. You shouldn't have any problems here. You will need to add one environment variable called SVN_EDITOR. This tells Subversion which editor to invoke when it's needed. This is used for small text edits, like code check in comments. Mine is set to:
SVN_EDITOR="C:\Program Files\Windows NT\Accessories\wordpad.exe"

Getting Started

We'll walk through creating a new repository, starting the Subversion server, add a user, import some code, and then edit the code. We'll see how to view a files history, make changes and get other developer's changes.


Make a repository

The first thing you'll need is a new repository. A repository is where all of your individual projects will be stored. Here's the exact command you'll need to run:

svnadmin create c:\svn-repo


Start a subversion server

Your Subversion client software will need a server with which it can talk. This is the svnserve executable. It's also possible to go directly to your file system and not use a server, but we're going to set this up so that you can access your files from other machines on your network. The -d option tells svnserve to run as a daemon.

svnserve -d -r c:\svn-repo


Add users to your server

In order to authenticate our users, we'll need to do some basic edits. Inside your new repo (c:\svn-repo) there is a directory called conf. There we'll find svnserve.conf. Open this file in a text editor.

For the moment, let's ignore all of the comments and just enable these lines:

[general]
anon-access = read
auth-access = write
password-db = passwd

You can leave all the comments in place, but I stripped them out of this example just to make it easier to read.

This format lets anyone read the files but only an authenticated user can check them back in again. It also tells the server that all the user names and passwords should be stored in a file called passwd. Let's create that file now. It should also be in the conf directory and look like this:

[users]
jared = shipit

Add as many users as you'd like in the format NAME = PASSWORD. There are more sophisticated ways to add password protection to Subversion, but this one looked the easiest to me. Apparently Subversion integrates nicely with Apache's web server authentication, so if you've got that set up already you can leverage that installation.

Using Subversion

Import a project

It's time to add some code to our new repository. In this example, I'm importing a directory called "junit" and my server is named "opteron1". (If you need a small project to import, I suggest you visit my short JUnit tutorial and download the code sample.)

svn import junit  --username jared --password shipit svn://opteron1/svn-repo


Checkout the project we just checked in

What good is a source code management system if you can't get the code back out again? This will look a lot like the previous command.  Before you type this command, go to a new directory. I made a directory called c:\svn-temp

svn checkout --username jared --password shipit svn://opteron1/svn-repo

Checking out the code creates a local copy for you to edit. Go ahead and make some minor edits now and in the next step we'll let Subversion show you the changes.


See your changes

Once you've made changes to your local files, you can see those changes very easily. It's also a good idea to review your changes before you push them back up to the server, just to be sure you remember everything you changed!

svn diff
svn diff <file name>
svn diff <directory name>

Put your changes back in the server

Once you've made some improvements to the local code that you checked out, you'll want to put that code back on the server. The server will keep a complete record of your changes, including the date and time of the change, who made the change and the changed code itself.

svn commit
svn commit <file name>
svn commit <directory name>

Get everyone else's changes

When someone uses the commit command to push their changes up to the server, you'll want to pull those changes down to your local machine. You do with the update command. As you're probably noticing by now, the commands are pretty similar. Once you get the hang of one or two of them, the rest start to make sense.

svn update
svn update <file name>
svn update <directory name>

Adding a new file

Even after you've imported your project, you'll eventually need to add a new file. One word of caution though. Unlike CVS, Subversion adds are recursive! If you add a directory with Subversion, it will add everything in the directory as well.

svn add <file name>
svn add <directory name>

See a file's history

If you want to see whose edited a certain file, or a directory tree, the log command is what you need.

svn log
svn log <file name>
svn log <directory nam>

An Alternate Interface

Once you understand how to use Subversion and you've at least seen the command line options, try out a GUI client. These days I'm using TortoiseSVN because it integrates nicely with Windows Explorer. Some people prefer a stand alone client and I suggest RapidSVN for them. Of course, every major code IDE has built in Subversion support.


Summary

There you have it. You can install the software, import projects, add files, see your changes, see other's changes, and put your changes back into the system.

This is not a complete tutorial, but it should be enough to get you started. If you do decide that Subversion is something you want to look at further, see the Subversion home page, the free Subversion Red Book, or pick up a copy of Pragmatic Version Control using Subversion by Mike Mason.