How to "un-commit" a revision from the top of a Subversion repository

Sometimes it may occur that a commit was performed in a subversion repository, that was not exactly what the original intention was. In such cases we would like to have an “undo” operation and bring the repository back to its original state, as it was exactly before the inadvertent┬ácommit.

The title of this post might be misleading, as technically there is no way to just un-commit or undo an executed commit from a subversion repository; after all, such feature would automatically cancel the very definition of version control.

Below I will describe the second best solution (or work around, if you prefer) on removing the last commit(s) from a subversion repository, which is to create a new repository and replay all the commits from the beginning and up to the revision just before the accidental commit. When done this, simply replacing the new repository with the old one will give the illusion that the unwanted commit(s) have been undone.

In this guide I assume that you have local command-line access and sufficient permission to the repository folder. I also assume that you wish to uncommit the last commit from the top of the repository. Once other commits have been performed, things get complicated and the procedure described below is not applicable. If the repository is used by multiple users make sure to ask from your peers to cease any commits until you are done with reverting the repository state.

First, you need to create a new empty repository:

giannis@ubuntu:~$ svnadmin create new-repository

Then, you need to dump all revision history from the original repository, starting from revision 1 and go up to the revision number that was exactly before the unwanted commit (typically this will be the current revision number minus 1).

giannis@ubuntu:~$ svnadmin dump old-repository -r 1:XXX > old-repository.dump
. . . . .
. . . . .
. . . . .
* Dumped revision XXX.

In the above command make sure to replace XXX with the current revision number decreased by 1.

Then you just need to load the dump file into the new repository:

giannis@ubuntu:~$ svnadmin load new-repository < old-repository.dump

Alternatively, it is possible to perform both dump and load in one single step, without having to create the intermediate dump file:

giannis@ubuntu:~$ svnadmin dump old-repository -r 1:XXX | svnadmin load new-repository

Finally, you just have to replace the new repository with the old one. Before doing so, it would be wise to create a backup of the original repository, just in a case something goes wrong.

giannis@ubuntu:~$ tar cvzf old-repository-backup.tar.gz old-repository/
. . . . .
. . . . .
. . . . .
giannis@ubuntu:~$ rm -rf old-repository
giannis@ubuntu:~$ mv new-repository old-repository

And you’re done! The advantage of this method is that you completely remove the accidental commit from the history of the repository, unlike other methods which simply merge the repository back to a previous revision.

I have done this many times and it helped. I hope you will also find this helpful. :-)

This entry was posted in Uncategorized. Bookmark the permalink.

One Response to How to "un-commit" a revision from the top of a Subversion repository

  1. RadioSite says:

    This site certainly has all the information I needed about this
    subject and didn’t know who to ask.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>