Rollback in Mercurial

Lets start with assumption that you are both user of Mercurial and that you made an error in merging. Or it might be directory restructuring gone bad. In any case, you need to go few revisions back.

Let's assume that we have small project well into development. Situation is simple enough and there are no branches involved. Only important thing to notice is that version that works was 137 and all changes after that are something that we would rather forget:

140: Some editing.
139: Fixing renames.
138: Directory reorganization.
137: Everything is working!

There are two solutions that can bring us toward situation as it was in 137.

Easiest, and (in my opinion) cleanest solution, is just to clone your repository. Making new clone that goes to revision 137 will solve problems with a clean cut. Unfortunately there are couple of problems with this clean solution. If your repository is shared one, be prepared to call every person that uses it and to tell them they need to delete their local repository and make a new clone. If you forget to do that changes that you just removed might magically appear when someone else pushes (since Mercurial will think that they have newer version). Some sort of synchronization is definitely needed.

Another issue with this method would be loss of history. If you had any changes done between 137 and 140, they are forever gone. That might not be a problem if there wasn't many changes or if they were trivial but you should be careful about this.

Fortunately Mercurial has way of solving this and it is called backout. Just remember to give it revision where you messed up. In this case that revision would be 138 (directory reorganization).

This method is not as nice since all that "bad" code is there. However, all history is preserved and all changes that happened after revision that was backed out are merged again. While it still might be a good move to notify team of changes, nobody will kill you if you forget to. :)

Leave a Reply

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