Reverting Changes in SVN (or: it's not as easy as svn up -r)

February 08, 2008 at 11:57 AM | DrProject, Python, Work | View Comments

If there is one thing that I've found universally confusing about version control systems, it's how to revert back to a previous revision, then move on from there. Conventional wisdom would dictate that svn up -r $OLD with some additional flag that says pretend that we're still at HEAD would do the trick... But, alas, there exists no such flag.

So, besides svn cat -r $OLD $FILE > $FILE, what's the best way to revert a file (or entire tree) to an older revision? Well, it turns out that merge is the tool you're looking for.

The trick is that you can diff backwards as well as forwards:

[wolever@thebes] ~/test_dr/All svn diff -r 2:1
Index: stuff
===================================================================
--- stuff       (revision 2)
+++ stuff       (revision 1)
@@ -1,4 +1,3 @@
 Let us endeavor so to live that when we come to die even the undertaker will be
 sorry.
                -- Mark Twain, "Pudd'nhead Wilson's Calendar"
-Increased knowledge will help you now.  Have mate's phone bugged.
Index: docs.html

Which means that merge will work both ways as well:

[wolever@thebes] ~/test_dr/All svn merge -r 2:1 stuff
U    stuff
[wolever@thebes] ~/test_dr/All svn ci -m "reverted changes" stuff
[wolever@thebes] ~/test_dr/All svn diff -r 1:3 stuff
[wolever@thebes] ~/test_dr/All

Of course, this will also work with the myriad of other version control tools out there. In fact, if you're still using SVN, reverting old changes is probably the least of your problems... At least compared to, say, merging different branches ^_^

But that's a post for another day -- I need to stop writing about merging and actually get on with, err, doing it...