22 Jun 2008

You would think that reverting to a previous version of your application would be one of the key use cases for a version control system like Subversion and that it would be trivial to do, but it's actually not intuitive at all.

When I was first starting out with Subversion, I thought there would be a revert command that took a revision as an argument and basically rolled back your application to revision X. Alas, there is a revert command in SVN, but instead of reverting to a previous revision, it works to undo any local changes you've made to your working copy.

Here, then, is a very simple, plain English explanation of how to revert to a previous version of your application in Subversion, to help anyone who may be starting out with it and is lost.

To revert to a previous version of your application (roll back changes) in Subversion, you merge the changes from your current revision back to the revision you want to revert to. So, for example, if you want to revert the trunk of your application from revision 73 to 68, you would do the following:

  1. svn merge --dry-run -r:73:68 http://my.repository.com/my/project/trunk
  2. svn merge -r:73:68 http://my.repository.com/my/project/trunk
  3. svn commit -m "Reverted to revision 68."

Step 1 will perform a dry run and show you what the merge will produce. If you want to see exactly what changes will be applied, do a diff:

svn diff -r:73:68 http://my.repository.com/my/project/trunk

Step 2 actually performs the merge (you'd do this after you're happy with the dry run). At this point, realize what is happening: Subversion is calculating the changes between revision 73 and revision 68 of the trunk and applying them to your working copy. For the majority of the time, you will thus want your working copy to be a fully updated copy of the revision you are reverting from (in this example, revision 73).

Finally, since the merge happens on your local working copy, you need to commit it to the repository in Step 3.

Add Your Comment

Spam Protection by WP-SpamFree

How to revert (roll back) to a previous revision with Subversion

  1. Aral, I am an SVN idiot so thanks for this. I have been using Version for the mac – http://www.versionsapp.com/ It is a free download while in beta, and super simple to use. Hoping they release it at a decent price.

    chris breshears
  2. This is great! I was until recently using ZigVersion for Subversion. Going to the command line has made me more efficient with svn, but damn if a GUI doesn’t make sense for some of it’s more confusing commands.

    Ben
  3. That’s why good GUI tools are important – this is a no-brainer in TortoiseSVN (view change log, right-click the revision I want to roll back to and select “Revert to this revision”). It does everything necessary under the hoods.

    Borek
  4. These is no good GUI apps for subversion on Mac (I’m not a Mac user so I don’t really care). Even the brand new Versions seems to suck. The only sensible thing to do is to use a combination of Eclipse/Subclipse. At least that gives you consistent quality over all platforms it supports.

    pan69
  5. Good to know, I’ve run into this one too. But can these commands also be executed in Eclipse, without using the terminal?

    Michiel van der Ros
  6. Hey Aral,

    If you haven’t already, you might want to check out Git. It’s a fantastic version control system (created by Linus Torvalds) that truly takes the pain out of merging (and many other things too)!

    I host my personal projects at GitHub which is also a great git resource for public and private repositories (I’m completely unaffiliated).

    Luke Bayes
  7. I find it easier to use the cat command to get the version I want minus all the merging. Yes, this is an annoyance with SVN.

    Something like:
    $ svn stat -u build.xml
    Status against revision: 17451
    $ svn cat -r 17450 build.xml > build.xml

    Then you can commit.

    Mike
  8. I started using Git recently and that is working really well for me too.

    David
  9. +1 From me for Git, I’ve not started using it but have researched it quite extensively and am very impressed with what I’ve seen and heard.

    DannyT
  10. Hey guys,

    Thanks for the nods for Git. I did research it before starting on the Singularity site but I figured that I had enough unknowns in the project — what with me re-learning Python after several years and learning Google App Engine — that I’d limit the risk but being conservative in at least one part of it :)

    I may transition to it once we have the first release of the site though — it does sound much better (and I like the fact that you can commit locally).

    Aral
  11. Hey Mike, I’ll check out cat, thanks — it looks easier than merge if you only need to restore a single file. :)

    Aral
  12. I was happy to find this today – thx! However, I think the syntax you want is -r73:68, not -r:73:68.

    Max
  13. Thanks for the post, it really helped me out. I’m using Versions.app for the Mac like someone above, but there is no merge option so these commands came in really handy.. Thanks again! Oh, and the syntax was -r 73:68 for my version of SVN.

    Sandro
  14. Thanks for the post.
    I can add that the SVN “bible”, “SVN redbook”, clearly states this usage of svn merge with fine details as well:
    http://svnbook.red-bean.com/en/1.5/svn.branchmerge.basicmerging.html#svn.branchmerge.basicmerging.undo

    Boaz.

    Boaz
  15. syntax error. change
    -r:73:68
    to
    -r 73:68

    ash
  16. thank you so much! You have saved me hours of reading.

    Victor
  17. Awesome…just what i needed… Thanks

    Vinayak
  18. Merci beaucoup :)
    (Thanks!)

    Amrac
  19. Gracias!!!!
    Thank you!!!

    El Demonio
  20. [...] whilst looking for a way to do this I came across a solution, which involes a revision merge and commit; basically taking your changes back to a previous [...]

    Miromedia Search Engine Optimisation Blog - Rollback a Subversion commit
  21. # revert back to revision in one line:
    svn update -r 2689

    petke
  22. Gr8 work, like it this way

    Mohit
  23. Thanks for the post, but the comment by petke worked much better for me – I didn’t have to resolve any conflicts during the rollback.

    Rob Searles
  24. Hi,

    Thank you very much for the information.

    Regards,
    Alan Haggai Alavi.

    Alan Haggai Alavi
  25. Thanks — this saved me quite a bit of work. I had to revert a change between two specific revisions that were NOT trunk. This worked like a charm.

    Nayan Hajratwala
  26. [...] Aral Balkan – How to revert (roll back) to a previous revision with Subversion (tags: svn revert rollback) [...]

    Steve Mactaggart’s Blog » Blog Archive » links for 2009-05-08
  27. Thank you very much :-)

    Sven Bachmann
  28. As far as I remember, you could just copy from an old revision to the HEAD revision, using the Repo browser. Unfortunately this is not possible anymore using the newest versions :-(
    It was a very simple but still meaningful way.

    Paul Pladijs
  29. Thanks for putting this up. Comment #20 above by petke works very well, too. I suppose that’s the way the developers intended for roll-backs, although I agree that “revert” seems more befitting than “update”…

    yungchin
  30. [...] I use this method on most my projects (private and freelance), e.g. I’m using it to synchronize my theme right here on satf. This is awesome because I tend to break stuff and it’s comforting to know that salvation is only one revert away. [...]

    Synchronizing Wordpress or website with SVN | satf
  31. Actually, petke’s way (using “svn up”) is *not* the same as using “svn merge”, and I don’t think it’s the way the developers intended for roll-backs. If you make changes after an “svn up” to a previous revision and try to commit them, Subversion won’t let you because your files are “out of date”.

    Nate
  32. Thank you for sharing this tip :)

    Tiago Almeida
  33. Thanks !

    If your working copy is on a network (it’s possible), you have to do like this :

    2. svn merge -r73:68 http://my.repository.com/my/project/trunk \\my.network.name\my\project\

    3. svn commit -m “Reverted to revision 68.” \\my.network.name\my\project\

    Alphonse
  34. Wait… can’t you just do “svn update -r “. That’s always worked for me…

    Chris
  35. Hello

    The command: svn merge -r:73:68… throws a “syntax error in revision argument ‘:73:68′ ”

    The first colon is a problem. svn merge -r 73:68… works fine.

    Aditya
  36. The command is svn merge -r 73:68 and not svn merge -r:73:68 as you have mentioned. There is no colon after -r and there should be a space instead.

    Vijay Dev
  37. svn merge –dry-run -r:73:68 http://my.repository.com/my/project/trunk

    should be

    svn merge –dry-run -r 73:68 http://my.repository.com/my/project/trunk

    John Malcolm
  38. Thanks for your very clear step-by-step explanation!

    Christian
  39. Thanks mate.

    Blah
  40. Thanks! This is just what I need. Anyway I use -rXX:YY not -r:XX:YY

    William Notowidagdo
  41. Thank you!! That was real useful! :)

    Sid
  42. I just found your post after searching for an easy way to revert to previous rev on SVN. Found the post useful, and it helped me greatly. Thank you.

    Just one thing I noticed in our version of subversion (1.1.4-2 .. very old) is that the command works best like this:

    svn merge -r 73:68 http://my.repository.com/my/project/trunk

    Khosrow
  43. Thanks for the article Aral. I wasn’t able to get the revision flag working as you have instructed however. I think the initial semicolon following -r may be incorrect syntax.

    *Instead of:*
    svn merge –dry-run -r:73:68 \ http://my.repository.com/my/project/trunk

    *Try this:*
    svn merge –dry-run -r73:68 \
    http://my.repository.com/my/project/trunk

    ferg
  44. Hi Aral,

    Great article – short, explaining, useful! Exactly what I was looking for.

    Thanks for that, and keep up the good work :)

    Cheers,
    Konstantin

    Konstantin Boyanov
  45. Thanks for sharing. This was driving me nuts trying to figure out how to get going.

    Michael Rice
  46. Thanks, quick and handy.

    Andrew Blair
  47. Very useful. The svn merge command is probably the most powerful in subversion. Love it.

    Geddon
  48. Just to make a note on ‘petke’ comment. I just want to make sure people reading this dont think you are actually correct. The way explained on this tutorial is 100% the right way to do it. Petke way is just an update to a particular version. It is not a true revert, is just updates your local copy and does not affect the repository.
    It is mainly use to viewing or grabbing past changes. I mainly use it for reverting one file at a time, as it is faster.
    svn update -r 4 (on file)
    -copy contents of r 4 of file
    svn update (repository version, lets say 10)
    -paste contents of r4 to latests
    svn commit -m ‘revert to r4′

    now you would be at r 11.

    Geddon
  49. It’s important to emphasize that any new changes you’ve made that have not committed will be lost when you merge your old revision in with your working copy.

    isam
  50. also, the syntax is -rN:N instead of -r:N:N (svn 1.6.5)

    isam
  51. Please could you update the correct commands.
    svn merge –dry-run -r73:68 http://my.repository.com/my/project/trunk
    svn merge -r73:68 http://my.repository.com/my/project/trunk

    Jimish Shah
  52. try this:

    svn up -r[insert revision #] [insert filepath]

    jeff
  53. thanks. it helped. for me though its not
    -r:73:68 but
    -r 73:68

    shridhar
  54. @pan69

    Cornerstone imo is the best Mac subversion GUI. You can do anything with it other than merging, but I prefer to do that kind of thing via cmd line anyway. It is most useful for visualizing changes, conflicts, viewing logs, etc. I like it a lot better than Versions and it is continually getting development updates especially now that it has a proper team working on it.

    danielsdesk
  55. Thanks for the instructions!
    The syntax is different for svn 1.x though. In particular:

    svn merge -r 73:68 http://my.repository.com/my/project/trunk

    lensovet
  56. Dude, your site keeps saying my comment looks like “spam”.

    Kalle
  57. Thanks Aral, very useful. Unbelieveably it’s only after five years of using Subversion that I’ve finally needed to revert to a previous reivsion. Hopefully that say something positive about my coding. Just to note that the above did work, although I had to remove the first semicolon from the revision argument:

    svn merge -r73:68 http://my.repository.com/my/project/trunk

    Jamie Saunders
  58. what’s wrong with…

    svn update -r 73

    …?

    Andrew
  59. svn merge -c -NUM .

    (svn 1.6 and onwards)

    Best of all...