47

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.

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
andreliebschner
  • 535
  • 4
  • 7
  • This was at least part of TortoiseHg before the new port, and is currently not implemented in the new port. See https://bitbucket.org/tortoisehg/thg/issue/188/what-happened-to-change-selection-in-the – Lasse V. Karlsen Oct 14 '11 at 09:09
  • 1
    @LasseV.Karlsen: The shelve feature is still available in the latest TortoiseHg. It can be accessed via the `Repository -> Shelve` menu or through an icon in the commit window. – Tim Henigan Oct 14 '11 at 13:33
  • Possible duplicate of [Mercurial cherry picking changes for commit](http://stackoverflow.com/questions/854930/mercurial-cherry-picking-changes-for-commit) – Emilio Miralles Apr 13 '16 at 00:41

4 Answers4

34

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.

jthetzel
  • 3,603
  • 3
  • 25
  • 38
bjlaub
  • 2,707
  • 21
  • 12
26

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.

IBBoard
  • 909
  • 4
  • 16
Paul S
  • 7,645
  • 2
  • 24
  • 36
20

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
Robert Siemer
  • 32,405
  • 11
  • 84
  • 94
8

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

Tim Henigan
  • 60,452
  • 11
  • 85
  • 78
  • 4
    The advantage of the shelve approach is that you can test your changes before committing. – Laurens Holst Oct 17 '11 at 08:29
  • 1
    +1 Laurens, It's always bugged me that when doing a 'git add -p', I'm essentially guessing what subset of changes will form a working commit, and then inputing that guess using an error-prone manual process. Asking for trouble. – Jonathan Hartley Jun 21 '13 at 08:22