tags:

views:

158

answers:

2

I made a change to a file in my mercurial project that I'd like to roll back, but I'd like to keep some changes that occurred subsequent to it:

Revision 1: Original
Revision 2: A change I made in error
Revision 3: A change I want to keep

I want to roll back ONLY the changes in Revision 2, leaving 1 and 3 applied, and create a new revision 4. What is the most "Mercurial" way of doing that?

Note that yes, I could extract the reverse diff from revision 2 and apply it to my working copy as a patch, and commit that. I know. But I'd like to find some way that the tool directly supports.

+5  A: 

I think that if you view the changelog for the project (so that you can see all of the revisions), you can pick a single revision and use the backout command to undo it.

(I don't have anything at hand to check this, so it may be incorrect.)

adrianbanks
Fantastic. Thank you, I clearly just overlooked that.
Chris R
+1  A: 

You can use mq patches to edit your recent history as well. The following sequence of commands would do the trick:

hg qinit               # initialises a queue
hg qimport -r2:TIP     # moves revisions from 2 onwards into patch management
hg qpop -a             # unapplies patches, leaving your actual checkout at r1
hg qrm 2.diff          # deletes the r2 patch (forever)
hg qpush -a            # applies all remaining patches again (i.e. the r3 patch)
hg qfinish -a          # moves applied patches back into official repository history

This sort of thing is great for fixing up a mistake in your local repository, but since you're changing history, make sure you didn't push to someone else (or they didn't pull from you) the original version before you fixed it.

If I understand correctly, hg backout will instead commit a new patch which reverses the earlier change, thus keeping the change in your history. It's a safer option, but less nice if you make local mistakes often (like me).

Lars Yencken