Subversion is one of the most powerful open-source version control systems available on the net, but it can often be intimidating for new users to get to grips with. Find out how, and more importantly why you should use Subversion in this tutorial.
We all know the scenario. You've been painlessly coding away at a file for ages. You make a last minute change to that uber-special design that's been eating away at your social time. You update that tiring documentation you've been forced to write.
Then comes the horrible bit. You're excited, possibly nervous. You save-as, navigate to the desired directory. Then reality bites. Filtering through hundreds of folders, deciphering the cryptic, confusing file names – suffixed with enough question marks and scary titles to make David Blain look timid.
It's all such a mess.
This is where Subversion steps in. Subversion, or SVN is a powerful version control system which makes it really easy to organize your workflow and work on collaborative projects simply and easily. Gone are the days of version headaches and deathly file names. SVN clears your head and allows you to focus on the project at hand.
Installing Subversion, on whatever platform of your choosing is a really easy process. If you are using Mac OS 10.5+, you can skip this step as Subversion is already installed, and it may be on other platforms.
If you are running linux, or a similar *NIX based system, the following command should work (replace
with your package manager):
sudo apt-get install svn
This should download, compile and install Subversion onto your system. If you are running windows, you can get the latest binaries from here. Download the installer, run and SVN should be installed on your system!
To check that SVN is installed and working, run this simple command at the shell:
If everything is successful, you should get an output similar to the following:
I'm gonna be using some terms and phrases specific to Subversion throughout this tutorial, so here is a quick glossary to make sure you can understand any terms you may come across.
- Repository (or repo) – the location on the server (accessible by SVN) where files are stored;
- Commit – when you send changes on your local copy of the repo to the remote repo;
- Checkout – creating a local copy of the repository;
In SVN, when you want to access the files in the repository, you need to check out the remote repository, and you are essentially creating a copy of the remote repo. Checking out a repository is really easy, you just have to know the URL (which will be found on many 'download' pages on sites, or under the 'Source' tab in Google Code). For this example, we will be using the SVN repository of Wordpress, a popular open source blogging system.
The URL for the Wordpress repo is http://svn.automattic.com/wordpress/trunk/, so you'll need to copy that (or remember it).
Open a terminal window, and type the following command:
svn checkout http://svn.automattic.com/wordpress/trunk/ /PATH/TO/YOUR/HTDOCS/SITES/DIRECTORY
Obviously you need to replace the /PATH/TO/YOUR/HTDOCS/SITES/DIRECTORY with the path to the directory you want to check out the source to, and hit enter. This should show lots of files being downloaded.
After this process is finished, navigate to the directory that you typed and you will see the entire Wordpress system! Well that was easy!
Creating Your Own Repo
As we can't make changes to the Wordpress repository, we are going to need to create our own repository so that we can commit, and test about with SVN's other features.
To create a SVN repository you can use the svnadmin command line tool. Open a terminal, navigate to the directory you want to open the repo in, and run the following command:
svnadmin create .
And that has created our repository on our machine. Please bear in mind this won't actually work from remote machines as you will have to configure a firewall, open network sockets and a lot of other stuff and this is outside the scope of this tutorial. However, we can still access it on our local network.
Adding Files and Committing
Firstly, checkout your newly created repository by running the following command:
svn checkout 127.0.0.1 .
…and create some blank text files, some images and some other files/directories in the local copy. Add them all by running this command:
svn add .
This should add the entire directory, but then you need to commit that to the repository to see any changes reflected across the network of checked out repos. You can commit it using the commit function, and optionally add a message using the -m operator:
svn commit -m "Added lots of files for tutorial"
This should then upload the files to the repository.
Updating and Changing
You can now make certain changes to the files in your local copy and subversion will intelligently pick up the changes and update only the new files when you next commit. The same works with updating.
Commit again by running the svn commit function again. This will then upload the files to the repository and
Updating a repository is easily done with the update command. This will query the Subversion repo and find out what new files have been updated, and then update them! This saves your computer downloading the entire repository in one go – especially with big repositories such as wordpress this can be really helpful.
The command's syntax is so:
And the system will do the rest.
This is where SVN really starts to get useful. You have full control over your commits, and the Subversion server keeps a history of what changed between each commit, allowing you to “roll back” to a previous version if something goes wrong.
Like everything else in SVN, rolling back requires one command:
svn merge -c -REVISION_NUMBER
Simply call merge and enter a previous version number and it will roll back to that version.
Another great thing about SVN is that you can easily see what differences there are between the current version on your local, and the most recent version on the repo – allowing you to look at the changes between revisions.
To do this, you can run the diff command:
That's all, folks!
So, hopefully you will have learned the basic idea of how to use Subversion. A definitive guide to SVN – The Subversion Book – is free to read online and highly recommended.
- Subscribe to the Theme Forest RSS Feed.