Every now and then you mess up and commit something that shouldn’t have gone out or you broke something with a bad merge. Undoing the damage is usually quite simple.
I’m going to use a sample case to explain the process. Let’s assume I have a branch called “bigchange” and I need to merge that to trunk and get it released on the web. Ok, so I do my standard svn merge and then commit my change. Then I realize that the branch had a piece of code with a syntax error in it and it breaks my entire website. I need to roll back that commit immediately from trunk.
In order to undo a commit, you need to either now the revision number to which you want to revert or you can just revert to “the previous” revision, but in that case you need to be sure that the “previous revision” is really where you want to go. Usually reverting to the “previous revision” is fine if you are the only one committing things to the repository at the time of the mess up.
To undo the commit that introduced bad code to the trunk, I would use the following command to revert to the previous revision.
svn merge --dry-run -rHEAD:PREV https://example.com/svn/myproject/trunk
That command reverts trunk from HEAD (the current revision) to PREV (the previous revision). You may have already noticed that the command has “–dry-run” in it. You should always dry-run your svn commits and merges. Dry-run will show you what the command will actually do. If the merge looked ok and the right stuff was reverted or undone, take out the –dry-run and run the real thing and commit your change.
svn merge -rHEAD:PREV https://example.com/svn/myproject/trunk
svn commit -m "reverted bad code with syntax error"
Sometimes you run in to an instance where you have made multiple commits and you need to roll back further. In this case, you need to know the revision number that you want to go back to. You can use the following command to browse for revision numbers.
Once you know the revision number (1005 for example), revert to it with the following command:
svn merge --dry-run -rHEAD:1005 https://example.com/svn/myproject/trunk
There are some other parameters you can pass to the -r attribute, check them out with: