What is svn+ssh? How do I use it to securely keep track of code?

So you want to know about svn+ssh, eh? Maybe some basics will help first. I’ll try to (briefly) explain what SVN is, why you use it, and why you want to let go of your 10-foot poll when working with SVN and SSH combined.

What is this SVN and SSH thing of which you speak?

Subversion (SVN) is a type of version control. When multiple people work on a development project (or one person makes a lot of mistakes), code needs a way of knowing who is editing what, when, and why. The versioning control software takes notes whenever source code is changed. It also makes sure two people don’t alter the same piece of code (because then it’s just awkward). To develop software in this way, two versions of the same code exists. There’s one copy stored in a central location called a repository, and another copy stored locally for you to edit and modify. First, you check out a copy of the source, which means you keep it locally. Then you do whatever you want to it. When you’re done modifying your local copy, you commit changes to the repository (that’s how the versioning control software keeps track of what is done).

Anyhow, back to the main topic. Lets say that you’re developing a web site that multiple people modify. Lets also say you use subversion for versioning control (or your system administrator does). It makes the most sense to have the repository on a central server. Your project is likely going to be hosted on a server, so the code itself might as well be kept on a server too. Now we get to the main purpose of this entry: how do you get your local copy from your hard drive to the server (and vise versa) securely? SVN+SSH.

What secure shells (SSH) are and how they work is beyond the scope of this post (try Wikipedia), but you’re basically using secure software over a secure connection to login to a remote computer. And it’s free. Those smart guys at CollabNet (the developers of subversion) thought that security must be important since so many people spend so much time thinking about it. They decided to integrate SSH into SVN.

How does one utilize SVN with SSH?

So lets quickly recap. SVN is for source revision controlling. SSH is for secure logins to a remote machine. Add the two together (with, say, a plus sign), and you get a secure way to do source revision controlling on a remote system! Nifty, no?

Now you may be thinking, “secure shells are complicated. Isn’t there an easier way to use SVN without being so draconian?” The answer is yes, but that’s not what this article is about. But don’t be scared. I already mentioned that SVN has SSH built in. It’s all done with the terminal command svn. Handy!

Checking Out Code Locally

First, you need a local copy of what you’ll be modifying. To checkout source code, do the following:

  1. Open your favorite terminal (mine is iTerm).
  2. Find a nice suitable location for your local copy of the code. I’ll use ~/code/, but you can use whatever you want.
    $ cd
    $ mkdir code
    $ cd code
  3. Now you download a copy of the code. You need two things: a valid login to the remote server and the repository location on the remote server. Lets say the remote code is stored at /svn/project1.
    $ svn co svn+ssh://jtv4k@jtlabs.net/svn/project1 .

    The co is short-hand for checkout, jtv4k is the username, jtlabs.net is the host, and /svn/project1 is the path. Simple, no? Substitute as you will.

  4. Enter your password when prompted. Watch the files fly by! Viola!

Committing Changes to the Server

Once you’re done doing, well, whatever it is you’re going to do with your code, it’s time to commit the changes. This is a pretty darn simple process. Remember that we’re keeping the local copy at ~/code.

$ cd ~/code
$ svn commit -m "I like cheese."

Done. The -m part means message. You want to let others know why you’re modifying the code. If there are any issues with your changes overlapping with somebody else’s, you’ll be prompted.

Keeping Things Current

Depending on what you (or more precisely, other people) are doing, that version sitting on your hard drive can get out-of-date pretty fast. You want to keep your copy as up-to-date as possible so you don’t get conflicts later on:

$ cd ~/code
$ svn update

That’s it!

Putting It Together

Subversion is pretty simple. It’s amazing how well it simplifies work done by multiple people. Just a few commands can do a lot. And if you get lost, try svn help. There are many more commands and protocols. Google helps with that.


Leave a Reply