Tutorials

SVN HowTo

How to use Version Control with Subversion (SVN)

Subversion is a free software that allows to manage files and directories on a server. Multiple users can work collaboratively with different versions of the files on the server.

First, start reading the SVN Book.

SVN Installation

To work with SVN, please install it depending on your operating system:

  • Ubuntu: sudo apt-get install subversion
  • OpenSuse: sudo zypper install subversion
  • Mac: The SVN command line tools are contained in the XCode installer.
  • Windows: Use TortoiseSVN!

SVN Usage

You can develop code and maintain it in the SVN repository in the following way:

Browse the repository online:

Check out a repo from the server to a local directory:

 svn co url/repo directory

For example:

 svn co http://svn.code.sf.net/p/libsquish/code/trunk checkout-dir

This is called an anonymous checkout, because local changes of the repository cannot be submitted to the server.

Change your working directory to the checkout directory:

 cd checkout-dir

Examine the files in the directory:

 ls -l

If the directory contains an open-source software, we can build the software, for example with the CMake build system:

 cmake . && make

Or the Automake build system

 ./configure && make

This is what you basically can do with an anonymous checkout.

If you do not already have write access to the repo, approach the admin of the SVN server and ask for permission to do so or create your own repository. I recommend sourceforge.net for that purpose.

If you have permissions to modify and check out the repository with a username and password, we can do much more with it:

First we check out a local copy with:

 svn co --username=username --password=password url/repo directory

Now we can modify the repository, for example by creating a file with a plain text editor and adding it to version control:

 emacs filename.txt &
 svn add filename.txt

Or modify the repository by creating an empty sub-directory and adding it to version control:

 mkdir subdir
 svn add subdir

This results in a modified local copy of the repository, but the repository on the server has not changed yet! The repository is changed only by committing the local changes:

 svn commit

For each commit you need to provide a comment, the so called commit log or commit message. The best way to specify this message is to type it on the command line after the -m option:

 svn commit -m “commit message”

This creates a new revision of your code on the server. Versions are numbered sequentially, starting with revision number 1.

Now modify your file again and show the changes with:

 svn diff

The changes shown by the above command are the differences of your local copy against the latest version on the server.

If you are done with your changes, commit those to the server:

 svn commit

Each new commit will result in a new version with increasing revision number. This time it’s revision number 2.

Side note: svn stores your password so that you do not need to retype the password arguments every time you commit.

Check and see the corresponding commit log:

 svn log

To update your local copy with changes others may have committed to the repository:

 svn update

If you are working on a project alone, you do not need to do the latter. But to be able to track the evolution of your code in a reasonable way, you should adhere to the following rules:

When working with SVN, there are four main rules of thumb:

  • Commit your changes at least once a day!
  • Do not commit code that doesn’t compile!
  • If it doesn’t compile, fix it!
  • Before actually committing it, review your code with “svn diff” and fix what you have forgotten to fix!

That’s it. You are using (sub)version control!


SVN Usage example

To check out the existing repository hello, which contains the “hello, world!” C example, type those commands in the unix shell:

 # check out repo
 svn co svn://schorsch.efi.fh-nuernberg.de/hello hello

 # change working directory to repo
 cd hello

 # show commit log of committed revisions
 svn log

 # compile and run hello-world example
 gcc hello.c -o hello && ./hello

 # modify contents
 echo “/* this is a comment */” >> hello.c

 # show line by line modifications
 svn diff

 # show modified files
 svn stat

 …

Options: