Make old svn revision the current revision

I ran across an issue that my google-foo has had some trouble handling. Maybe what I did is the only way to do it, in which case maybe this will help someone in need… but I rather like to think that someone here will have a much nicer solution.

I use Subversion at most of the clients I work for, for most of the projects I work on. It’s even used in the production/editorial process at both Python Magazine and php|architect Magazine. For my own work, I’m still using it personally mainly because I haven’t had the time to really get a good grip on Git yet.

Though I’ve been using svn almost since it was created, I can’t find a clean way to revert to an earlier revision of a file, and then make that version of the file the new “current” version of the file. At first I thought I could just make a trivial change to the file and commit, but you can’t commit if the file is out of date with the repository (that’s why you’re supposed to run ‘svn up’ before committing – duh). I also didn’t see an option to any of the svn commands that looked like it would work, but maybe I missed something (do tell!).

What I wound up doing was moving the local copy of the file sideways, svn up’ing the file back down, moving the good copy back over (overwriting the one I just svn up’d), and committing. Nasty. It looks like this:

$ mv myfile.php myfile.php.bak
$ svn up myfile.php
$ mv myfile.php.bak myfile.php
$ svn commit myfile.php -m "All cleaned up"

It works, but it just seems like something that should be built into svn somehow. Those with sufficient clue are hereby solicited to share it :-)

Tags: , ,

  • Hans Sjunnesson

    svn help merge

    Basically, merge an old revision with HEAD, this will mark the file as modified, and then commit.

  • Hans Sjunnesson

    Actually, to make it a bit more helpful:
    svn merge -rHEAD: myfile.php
    svn commit myfile.php

    old_revision in this case can be a revision number or a date, or any of the other defined keywords you’ll find running svn help merge

  • Hans Sjunnesson

    Actually, to make it a bit more helpful:
    svn merge -rHEAD:old_revision myfile.php
    svn commit myfile.php

    old_revision in this case can be a revision number or a date, or any of the other defined keywords you’ll find running svn help merge

  • http://thisfred.blogspot.com eric casteleijn
  • http://thisfred.blogspot.com eric casteleijn

    And in a synchronicity fluke, I just accidentally happened upon:

    http://philikon.wordpress.com/2008/05/29/eazysvn-makes-subversion-saner/

  • https://www.coderesort.com/u/simon Simon

    The trick is actually to use svn merge, and reverse the arguments to do a ‘negative’ (reverse) merge – as opposed to the usual forward merge where one compares old:new.

    svn merge -r HEAD:247 myfile.php

    Remembering that a changeset of a given number takes the revision of the repos to the same number, this will reverse any changesets down to (and including) 248 – but not 247 itself as that change is a delta against 246.

    For any one-off mistakes, this can be simplified for individial changesets like this (this time just changes from changeset 248):

    svn merge -c -248 myfile.php

  • m0j0

    Thanks, everyone, for the helpful comments. I’ll now have a point of reference for when I forget how to do this next time ;-P

    I had checked out svn merge, but I guess it just didn’t seem intuitive to me, but then it’s also not intuitive to me to use ‘update’ to revert back to an old version instead of, I dunno… ‘svn revert’?

    Live and learn. Thanks again.

  • http://billmill.org Bill Mill

    > svn merge, but I guess it just didn’t seem intuitive to me

    You and nearly everyone else who’s ever used svn. I hope you never have to resurrect a file (or, god forbid, a directory) that you used a long time ago. Especially if you’ve ever used locks at any point ever.

  • m0j0

    Thanks, Bill — nice to know I’m not alone :)

    As for old directories, I actually keep a disgustingly large archive of full svn backups, so maybe that could help me out. So far, test restores of those repositories seem to pass muster.

  • Mercurial fanboy

    If you’re trying to get into distributed VCS just take a look at Mercurial, it’s a lot simpler then Git and powerful enough for hardcore uses 😉

  • David Mellor

    svn cat -r old_revision myfile.php > myfile.php
    svn commit myfile.php

  • Tyler

    So, this may be embarrassing for me, but: what I did was accidentally remove folders of files, and commit the changes. So, I ran “svn up -r N folder/ folder/ folder/” then “svn rm –keep-local folder/ folder/ folder” followed by “svn cleanup” and finally “svn add folder/ folder/ folder/”. This seemed to work.

  • http://www.alessio.us alessio