Mercurial cherry picking changes for commit

mansu picture mansu · May 12, 2009 · Viewed 23.5k times · Source

Say, I made many changes to my code and only need to commit a few of those changes. Is there a way to do it in mercurial? I know that darcs has a feature like this one.

I know hg transplant can do this between branches, but I need something like this for committing code in the present branch and not when adding change sets from some other branch.

Answer

mwolfe02 picture mwolfe02 · Oct 5, 2010

If you are using TortoiseHg 1.x for Windows, this feature is implemented beautifully right out of the box (no extensions required).

  1. Run the TortoiseHg Commit Tool.
  2. Choose a file for which you only want to commit a subset of its changes.
  3. Click on the Hunk Selection tab in the preview pane.
  4. Double-click or use the spacebar to toggle which change hunks should be included in the commit.

For TortoiseHg 2.x, the Hunk Selection tab is now gone. In its place, is the Shelve tool. It has a few more features than the old hunk selection. Those new features come at the cost of some added complexity.

enter image description here

Note that there is no need to explicitly enable the Mercurial Shelve extension when using this feature. According to Steve Borho (lead TortoiseHg developer) in response to a different TortoiseHg question: "We have a local copy of the shelve extension and call into it directly."


For TortoiseHg 2.7+, this functionality has been improved and re-introduced. It is now built directly into the Commit tool:

example of change selection in the commit tool

Notice in the file list on the left that the top file is checked to indicate it will be included, the second file is unchecked because it will not be included, and the third file, Sample.txt, is filled (the Null checkbox indicator) because only select changes from that file will be included in the commit.

The change to Sample.txt that will be included is checked in the lower-right change selection portion of the image. The change that will be excluded is unchecked and the diff view is grayed out. Also notice that the icon for the shelve tool is still readily available.