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

8

21

I often notice a few unwanted changes when I review my working copy (with hg status and hg diff) right before a commit. For example, I might have temporarily added or remove some code just for the duration of a debugging session.

I know I can use hg revert to remove unwanted changes, but this removes all the changes in the entire file. Is there a way to revert just a part of a file?

Mansour answered 14/12, 2010 at 16:25 Comment(1)
Similar: #877263Equivoque
U
5

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.

Ubald answered 14/12, 2010 at 16:44 Comment(2)
+1 I've followed these instructions to enable the hg kdiff3 command, and I've found the "Merge - Merge this file" menu item. It took me a while to find that you need to right click conflicts to chose which of the two versions you want to select, but it seems to work. I'll give this a try.Mansour
"Merge Current File" was the missing piece of the puzzle. great thanks.Orgulous
A
8

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).

Arsenopyrite answered 14/12, 2010 at 19:53 Comment(1)
I use: hg record -m temp ; hg revert -a ; hg purge ; hg strip -k . to do exactly this, but also return the temporarily committed changes to the working copy after eliminating the unwanted changes.Gourmet
R
8

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.

Rendon answered 13/9, 2016 at 0:23 Comment(0)
B
6

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
Beatrizbeattie answered 4/6, 2014 at 10:9 Comment(0)
U
5

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.

Ubald answered 14/12, 2010 at 16:44 Comment(2)
+1 I've followed these instructions to enable the hg kdiff3 command, and I've found the "Merge - Merge this file" menu item. It took me a while to find that you need to right click conflicts to chose which of the two versions you want to select, but it seems to work. I'll give this a try.Mansour
"Merge Current File" was the missing piece of the puzzle. great thanks.Orgulous
M
3

The TortoiseHg GUI's "Commit" window has a "Hunk Selection" tab that allows selection of specific sections of a file's changes to be committed.

Mordecai answered 15/12, 2010 at 4:16 Comment(1)
@user2864740, revert the file and commit the hunks you want to keep.Mordecai
C
1

I've got a different answer for your problem, which is the same problem I have. This is a great use case for mercurial queues!

When I am about to start adding debugging code to a change that I think is ready, I do the following:

hg qnew -m "fix for bug #123" fix.patch  # basically a local-only commit
hg qnew -m "debugging" dbg.patch         # prepare the next changeset at the tip
[add my debugging]
hg qrefresh                              # update the changeset at the tip
[...]
hg qpop                                  # pop the debugging off the repo history

It takes a little bit of getting used to -- you end up having to reorder your patches to then fold whatever fixes you made into the original work patch.

Also, check out Bill Barry's attic extension. This page talks about how to use it for a few different workflows and how that compares to using mq. https://www.mercurial-scm.org/wiki/AtticExtension

Cervine answered 15/12, 2010 at 20:1 Comment(0)
B
0

If the commit where you want the change is e.g. ba1c841aaff4, the easiest is to use:

hg meld -r ba1c841aaff4^ <filename>

Now click on a right arrow (pointing to the right) in the middle to revert your lines, save the file and close meld. kdiff3 (old and unintuitive) can be an alternative.

Side note: in order to use meld you need to configure it in our ~/.hgrc file:

[extdiff]
cmd.meld =

[merge-tools]
meld.args=$base $local $other
Boneset answered 7/3, 2017 at 8:45 Comment(0)
S
0

Download and install meld https://meldmerge.org/ (You should have Mercurial installed locally or TortoiseHg)

  1. Launch Meld and select Version Control View and then select you will be asked to choose a parent directory of the file you want to modify. enter image description here
  2. Select the file which has the uncommitted changes enter image description here
  3. Choose which lines you want to revert to the last commit by selecting the arrow. The document on the left is from the last commit and the right document is the file with the uncommitted changes. enter image description here
Sainthood answered 7/10, 2020 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.