Reverse Commit Code changes in SVN

Print Friendly

SVN is one of the great tools when it comes to controlling the version of your code. Code can be committed at instances to the repositories and each change is saved as a change set. Sometimes while working with SVN, there comes a need for reversing a change. Rolling out a committed change set is not straight forward in SVN and may require some trick.

This can be really helpful in case when you are working on a repository copy like /trunk and you find the changes made by you in last revision or changeset (Say Changeset #783) are wrong or they should not have been committed. Now one of the solution in such scenarios can be making changes again to your codebase which revert the changes you just committed. That works, and i have done it sometimes. But, what happens if you have multiple files and many code changes that you commit in changeset #783. Or what happens when you have committed multiple changesets after that and are sitting on Changeset #783 may be.

For such situation, you can use svn merge to “undo” the change in your working copy, and then commit the local modification to the repository.

For this you need to do is to specify a reverse difference:

$ svn merge -r 783:782 http://svn.windowsvj.com/coderepo/trunk/library/
U badFile.php
$ svn status
M badFile.php
$ svn diff
# verify that the change is removed …
$ svn commit -m “Undoing change committed in r783.”
Sending badFile.php
Transmitting file data .
Committed revision 850.

By using the -r switch, svn merge apply a changeset, or a whole range of changesets, to the working copy. In this case, SVN merge is applying changeset #783 to the working copy backwards.

Keep in mind that rolling back a change like this is just like any other svn merge operation, so you should use svn status and svn diff to confirm that your work is in the state you want it to be in, and then use svn commit to send the final version to the repository. After committing, this particular changeset is no longer reflected in the HEAD revision.

Although the master branch is Good now, the original change still exists in the repository’s history.

In case, you find an error like :

svn: Cannot reverse-merge a range from a path’s own future history; try updating first

Try updating your working copy before making any commits.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
http://www.windowsvj.com/outlet