Uncategorized

subversion: How to revert a bad commit

Ever made that commit that messed up your repository and spent some time wondering how to undo, or revert a bad commit? In subversion you really can’t go back a revision, but you can merge your copy with the previous repository version and then commit the changes to correct your mistakes:

svn merge -r [current_version]:[previous_version] [repository_url]

svn commit -m “Reverting previous commit and going back to revision [previous_version].”

This is a life saver πŸ™‚

UPDATE: Julian found a ‘typo’ on this post. Corrected. Thanks Julian!
UPDATE 2: I found a horrible typo on one of my comments. Phew… πŸ˜› Anyway, I am glad that this post is helping people to resolve their problems πŸ˜‰

Advertisements

32 thoughts on “subversion: How to revert a bad commit

  1. Note that recent versions of subversion have added the -c shortcut to select one particular revision number. So the command can now be shortened to:

    svn merge -c -[bad_revision] [repository_url]

    Note the ‘-‘ vefore the revision number. It’s important.

    -c [y] is equivalent to -r [x]:[y]
    -c -[y] is equivalent to -r [y]:[x]

    We want the second variant here.

    Extra tips:
    (i) Note that reverting this way won’t restore your local copies to what they were before. It’ll revert them to [previous_version]. So you may want to make copies of your changed files before you do the revert.

    (ii) Don’t forget the handy ‘–dry-run’ flag if you are not sure you have got the command right. svn will report what the command is going to do, but it won’t actually make any changes to your repo or local copy.

    (iii) If, when you try to revert, you get a warning such as:

    Skipped missing target: ‘…somepath…’

    it means you have may have missed the branch part off your URL. You probably need to add a “/trunk” or a “/branches/yourbranchname”. For example:

    http://mysvnrepo.net/repo/foo/trunk

    instead of

    http://mysvnrepo.net/repo/foo

  2. “svn merge -c -REV” is good but the above command allows for reverting multiple revisions at once. Both have their place and time to use.

    Thank you, Lucas!

  3. Great, thanks.

    To explain sarniak’s short “this is better” message he suggests that you use the -c argument to only have to present one revision number and to do it with a ‘-‘ sign in front of the version to reverse whatever changes were made with it.

    Lastly, he would suggest just pointing to the local checkout with ‘.’ to avoid having to put in the whole URL.

  4. You *can* do this automatically in Tortoise SVN.
    Goto root directory, right-click>Tortoise>show log, select the revision, then right-click>revert.
    NB:You can’t get to this option by right-clicking a file>show log. MUST be done from the root of the project.

    1. Hey Robin,

      Thanks for the tip on using tortoise SVN, I use svn only on linux using the default command line client, so your tip is very welcome πŸ™‚

  5. This saved me from an insane situation! Thank you!

    I’ve been doing Git code management on my local machine and doing git svn dcommit’s to sync my SVN server. Never Do This On A Flaky Internet Connection! I was using my phone’s wifi hotspot when I only had a half a bar and the commit push to SVN died half-way! A disaster because the commit got to SVN but was never linked back to my Git repo and I couldn’t for the life of me get them back into an appropriate state.

    Checking out my SVN trunk separately, running your merge above, and then git svn rebase’ing again before committing saved my (code)life! Awesome.

  6. Hey, thanks very much! Also, it would be great if you add a real example, maybe ilustrating that we can use “.” as “repository_url”.

  7. A very simple thing, yet very useful. I intend to translate it to brazilian portuguese and put it in my own blog, giving the credits of course. Thank you!

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