Important Subversion Commands


Introduction

Briefly, subversion is a tool that allows you to keep track of all the versions of your code. It resolves a common problem programmers have. Consider this example. I'm working on my MP and I finish the first feature. Now, it would probably be a good idea to keep a backup of this version of my code (I might break it later, so I want to be able to revert my changes). I make a copy of my mp.c file and call it, mp_feature1.c. 10 features later, I have 10 files laying around. This is called a "nightmare."

SVN (subversion) lets you declare that you have made some changes, and hold on to that version of the code. This is called a "commit." Every commit is given a revision number, so if for some reason you need to go back to that version, you just revert to that revision of your code

How do I commit my changes?

The svn commit is how you tell svn to hold on to the version of the code you have sitting in front of you right now. Usually, you want to give a helpful message with your commits, like "Finished version 1" or "Killed some pathetic bug" or maybe even "Angrave is the best professor" although the last option will give you very little information about that commit. For more examples of bad commit messages, there are a variety of good resources if you Google "funny commit messages."

SVN commit pushes your changes to the svn server run by EWS, so your changes will be visible to the TAs and to the autograder

Here is an example:

$ vim my_file.c # edit and add cool feature
$ svn commit -m "Added a sweet feature"

Help! I broke my code since my last commit!

The svn revert command lets you revert back to the last committed copy of your code. So, say I'm writing some program that scrapes data from Twitter. Once I get the scraping part working, I commit my code. Then I start working on the data processing part and I totally screw the whole thing up. Since I committed earlier I can just drop all my changes and try again.

Here is an example:

$ vim twitter.c # edited to get scraping to work
$ make
$ ./twitter # it works!
$ svn commit -m "got scraping to work"
$ vim twitter.c
$ make
$ ./twitter # everything is broken
$ svn revert twitter.c
$ make
$ ./twitter # everything is the same as it was when it worked last time
    

Help, I'm committing code and nothing is happening!

This often happens because svn doesn't know it needs to track the changes made to your files. You can tell svn to track the files with the svn add command.

So, say I wanted to add the file sick_file.c to svn, I would do this:

svn add sick_file.c

Alternatively, you can add an entire directory. Here I will add "rad_directory" so svn will track all the files in the directory.

svn add rad_directory

How can I get the original version of the code? How do I get back to an old version of my own code?

Suppose you have totally fouled up your MP or discussion code, and you need to get some other version of the code back. To do this, you will need to switch back to an older revision.

A revision is any committed copy of code. If you want to display all the changes that have been made in the code, you can use the svn log command.

SVN log gets you a log of all your commits (with their revision numbers) of some object in the repository. This object could be a single file (you want to see changes made only to this file), or a whole directory. There are all sorts of fun options, if you want to see them, run a svn --help log

Here is an example from my cs241 repo from a previous semester:

$ svn log msort.c
------------------------------------------------------------------------
r7651 | zmick2 | 2014-03-06 00:23:20 -0600 (Thu, 06 Mar 2014) | 1 line

removed useless code
------------------------------------------------------------------------
r7416 | zmick2 | 2014-03-04 22:08:17 -0600 (Tue, 04 Mar 2014) | 1 line

making threads
------------------------------------------------------------------------
r7411 | zmick2 | 2014-03-04 21:46:26 -0600 (Tue, 04 Mar 2014) | 1 line

have segment splitting in place working
------------------------------------------------------------------------
r7368 | zmick2 | 2014-03-04 17:33:25 -0600 (Tue, 04 Mar 2014) | 1 line

reading from stdin
------------------------------------------------------------------------
r1368 | fanyang5 | 2014-08-28 19:27:10 -0500 (Thu, 28 Aug 2014) | 1 line

publish the mp
------------------------------------------------------------------------
    

Here you can see 5 commits. 4 of them are my changes to the code. The first commit (last one displayed) is the original commit of the MP. We will always put the word "publish" in the commit message when we publish the code for an mp or discussion.

Every svn log line with the vertical lines in it has a few important pieces:

r1368 | fanyang5 | 2014-08-28 19:27:10 -0500 (Thu, 28 Aug 2014) | 1 line

Meaning:

revision number | user making commit  | date of commit | 1 line
    

To rollback to the previous commit, follow these steps:

  1. Get the revision number of the commit you want to revert to from svn log
  2. Revert (or commit) any changes you have made since your last commit. See above notes in this web page for information about those commands.
  3. Get the code for the revision number you want by merging that version of the code with the current version of the code using this command: svn merge -r REVISION_NUMBER
  4. You now have reverted your code to the revision you wanted, it's a good idea to commit again to indicate that you have reverted, so go ahead and do that with svn commit -m "reverted to revision REVISION_NUMBER"

For more information see this stack overflow question: How do I return to an older version of our code in Subversion?