Reverting a Commit in Subversion

Once in a while, someone commits a revision that has to be rolled back later for some reason. Some teams do that as a matter of policy when people check in broken code that doesn’t compile or isn’t able to run all test cases successfully. Since Subversion (and similar revision control systems) are designed to never lose changes, this isn’t easily possible.

If a commit really has to disappear without traces for some reason, the only way is to import your repository into a new one leaving out the offending commit. That’s a rather drastic measure causing all kinds of problems with existing working copies, so use it with great care.

The approach used more often is to commit a new revision that takes back earlier changes. This is like undoing all changes manually and committing them in the usual way. The offending changes are still in the history, but don’t do harm anymore.

This command rolls back changes made in revision N, using a reverse merge:

svn merge -c -N .

Note the minus character in front of N. This is the same as the following command (which also works with older subversion clients):

svn merge -r N:N-1 .

Rolling back multiple changes (let’s say revisions LOW to HIGH, both inclusive) works the same way:

svn merge -r HIGH:LOW-1 .

Afterwards examine the changes made to the working copy and commit them.

To understand what’s happening here requires some background on what the merge command does conceptually: It records which changes have to be made to revision N to turn it into revision N+1 (called a delta) and applies it to the working copy. That’s not what we want in our scenario, so we reverse the usual order of revisions on the command line. Effectively, we tell the merge command to create a delta the other way round. The result is a "rewinding" of the code base.

This entry was posted in tools and tagged , . Bookmark the permalink.

8 Responses to Reverting a Commit in Subversion

  1. Sean says:

    Great explanation, here’s some extra help for Windows users:

    To do this with TortoiseSVN, you right-click the directory or file you’d like to revert and use “Merge” from the TortoiseSVN menu. Select the option “Merge two different trees”. Under “From” select the Head revision. Under “To” select the old revision + 1 (i.e. if you’d like to revert to revision 169, type revision 170 into the box).

    At the next screen, feel free to click “Test Merge” to have a look at your results before taking the plunge. If things look good, click “Merge” to finish the job.

    Finalize your changes with a commit and you’re done. :]

  2. mafr says:

    Thanks a lot, Sean! Now I’ve got something I can point my windows using colleagues to and don’t have to do the reverts myself :)

  3. Amiri says:


    Thanks for the Windows help, just found your explanation and it was exactly what I was looking for and it worked perfectly.


  4. Pingback: So Much for 2010 « Unmaintainable

  5. konqi says:

    Thanks, this really helped!

    Other sites recommend to revert, copy the files, update, replace and re-commit the files – which I find somewhat inelegant. This is a much more suitable solution.

    (Please don’t ever remove this post. I have bookmarked it so forgetful me can always return. :-))

  6. Pingback: Eclipse and Subversion: Reverting a bad commit

  7. Thanks, this was a feature I knew from another vcs (ClearCase) and could not find in SVN.

  8. Pingback: Andreas' Blog » Blog Archive » Subversion

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s