Can I revert a range of lines in a file with mercurial?

Assuming you have the record and shelve extensions enabled, you can do as follows:

hg record -m "garbage"   # pick out and commit the change you want to revert
hg shelve --all          # temporarily hide other changes
hg strip tip             # remove the garbage changeset
hg unshelve              # restore the changes you want to keep

One way is using a graphical diff tool like kdiff3. If you feed it the diff and choose "merge current file" you can go line by line and pick what you want.

The better way is to commit more often. If you make a habit to commit right before adding debugging code, then either commit or revert your debug code before adding your "real" code, it makes it very easy to remove your debug code because it has its own revision. Alternately, you can put your debugging code in a separate branch altogether.


I'm not sure if you can revert individual lines explicitly, but what I do in situation like yours is to commit the good code and revert the rest (the bad code). This workflow is easy using Mercurial's record or crecord extension (I recommend the latter one).


I've been doing this with the interactive ncurses style ui hg revert -i that lets you walk around and select the parts you want to destroy, either file, diff chunk or line by line, as you please, depending on how deep you unfold your changes.

I am not sure if this is a standard hg feature or not, you can verify easily enough if yours has it:

> hg revert --help --verbose | grep -- -interactive
 -i --interactive         interactively select the changes (EXPERIMENTAL)

Just remember that the changes you mark (X) will be what gets nuked, not what you retain.

Tags:

Mercurial