Where I put the clever stuff...

17 July 2008

Subversion (SVN) Notes

Subversion is a version control system you can use to maintain current and historical versions of files. It can be used for any type of file, but I’ve just started using it with source code as I develop websites.

As well as keeping a copy of all the files you create, subversion keeps track of all the changes you make to a file. So, if you embark on a massive code rewrite and it all goes a bit wrong, all is not lost. Subversion also helps when collaborating on code, or working from different locations, as all files for a project are stored in, sent to and retrieved from a single central store known as a repository.

Installation

To install Subversion on your debian server, you’ll want to do the following

apt-get update
apt-get install subversion

Next, create a folder to keep your repositories in, and give yourself the required permissions:

mkdir /var/svn-repos/
chown -R username /var/svn-repos/*
chmod -R 770 /var/svn-repos/*

Adding your first project

Use a project folder as it stands, or create a new one. For this example, I’m going to create a dummy project in my www directory:

cd ~/www
mkdir project_albatross
cd project_albatross
nano index.html

Now create a repository for the project:

svnadmin create --fs-type fsfs /var/svn-repos/project_albatross

And import the project so far into it:

svn import ~/www/project_albatross/ file:///var/svn-repos/project_albatross/trunk -m “Initial import”

Note the message attached to the import with the -m flag – these are important when reviewing changes. Make the message something useful. Also note the /trunk subdirectoy. This is where the main development version lives.

Now remove the original, unversioned project:

rm -rf ~/www/project_albatross

And “check out” the project from the repository:

svn co file:///var/svn-repos/project_albatross/trunk ~/www/project_albatross

This checked out version is known as your working copy. This is where you make edits, before “committing” them back to the repository.

So edit away!

Using SVN

When your done editing your files, type

svn commit -m “Message”

to commit your changes to the repository. If you want to see the changes made since you last committed your project, you can type:

svn status

If you’ve created a file, you’ll need to add it to subversion so that it keeps track of it:

svn add newfile

You’ll also need the following commands if you are altering the files involved in the project:

svn mkdir
svn copy
svn rm
svn rename

If you want to see the log of changes on your repository, enter:

svn log file:///var/svn-repos/project_albatross

Other useful commands might be:

Resources

What next?

It will be worth a look into setting up subversion to be accessible across the local network / internet, so that your project is available to you whatever your location. A future post, perhaps!

Comments

Commenting is closed for this article.