Revert or rollback a SVN revision

If you have ever deployed code from a SVN repository with a bug in it and you need to revert to a working revision then you may find the following handy.

First find the revision numbers for the file(s) in question.

Use the log command to get the info from previous commits including revision numbers.

So something like…

svn log update-data.php 
--------------------------------------------------------------
r4057 | eoin | 2010-03-25 17:33:32 +0000 (Thu, 25 Mar 2010) | 1 line

turn on db errors
--------------------------------------------------------------
r4056 | eoin | 2010-03-25 17:32:36 +0000 (Thu, 25 Mar 2010) | 1 line

add update file
--------------------------------------------------------------

Use diff command to see the difference between the 2 revisions.

svn diff -r 4056:4057
Index: update-data.php
=============================================
--- update-data.php	(revision 4056)
+++ update-data.php	(revision 4057)
@@ -6,7 +6,7 @@
 
-$db->show_errors = false;
+$db->show_errors = true;

Sometimes this is all you need, as you can see the mistake you made. Now you can make a change to the working copy and commit a fix. However if there were too many changes to do yourself, you may prefer to revert to the last known good revision. In this case, you will need to merge the changes from your current revision back to the revision you want to revert to.

You should try a dry run to see what a the outcome of a merge will be before you execute the real merge.

svn merge --dry-run -r 4057:4056 some_root_dir/update-data.php 
U    some_root_dir/update-data.php

If you get an error like..

svn: REPORT request failed on '/!svn/vcc/default'
svn: Cannot replace a directory from within

You will need to pass in a destination or working copy path to the merge command, so change the command above to…

svn merge --dry-run -r 4057:4056 some_root_dir/update-data.php some_root_dir/update-data.php 
U    some_root_dir/update-data.php

Now execute the merge command to revert to previous revision.

svn merge -r 4057:4056 some_root_dir/update-data.php some_root_dir/update-data.php
U    some_root_dir/update-data.php

svn diff some_root_dir/update-data.php 
Index: update-data.php
==============================================
--- update-data.php	(revision 4057)
+++ update-data.php	(working copy)
@@ -6,7 +6,7 @@
 
-$db->show_errors = true;
+$db->show_errors = false;

Use a diff to see the changes made after the merge and you should see that the change(s) made have been undone. Now commit the working copy to include this change and all’s well in the world again.

svn -m 'turn off db errors again' commit some_root_dir/update-data.php

One final point, be super careful with a merge, things can get real messy!
Take your time, make sure you use diff to see what changes have been made and check again when your through 🙂

Really good and detailed explanation of the revert process from aralbalkan.

2 thoughts on “Revert or rollback a SVN revision

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s