hg equivalent of git add -p?

Is there a mercurial equivalent of git add -p?

Quoting from man, git-add with the option -p (or --patch) does the following:

Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the difference before adding modified contents to the index.


Have a look at the record extension (which comes bundled with Mercurial).

Note that since Mercurial doesn't have the concept of the staging area like git, running hg record will simply allow you to examine, hunk by hunk, the modifications in your working copy. Any changes you choose to record will be committed, and any changes you choose not to record are simply left as modifications in your working copy.


The Record Extension is the standard tool for this. It allows you to pick hunks to include or not in a commit. Once you've enabled the extension in your hgrc, the command is just

hg record

The CRecord Extension gives you a TUI (Text User Interface) on top of this which allows you to go down to which lines you want to include. This isn't standard though, so it need downloading to a directory before you can enable it in your hgrc.

hg crecord

Edit:

  1. The Record extension is no longer necessary since approx v3.4. Now various commands support the -i or --interactive flag. For example:

    hg commit -i
    

    ...will ask you hunk by hunk what you want to include.

  2. CRecord made it in to 3.8 as a core feature. Add the following to your .hgrc

    [ui]
    interface = curses
    

    Now, --interactive commands will bring up the same interface as the old CRecord extension.


As of Mercurial 3.8.1 the crecord extension is builtin. Make sure the “ui” section in your .hgrc contains interface = curses, or you get an uncomfortable question-and-answer-interface.

[ui]
interface = curses

Then do your commits interactively to be asked about which hunk—or—which line you want to include in that commit.

hg commit --interactive

If you are using TortoiseHg, it has a Shelve feature which allows you to store changes you don't want to commit to a temporary area. It allows hunk selection, just like git.

In the TortoiseHg Workbench, this tool can be accessed in 2 ways:

  1. Click Repository -> Shelve
  2. In Commit window, select the shelve tool icon. It looks like this:

enter image description here