Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mercurial cherry picking changes for commit

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.

like image 292
mansu Avatar asked May 12 '09 21:05

mansu


People also ask

How do you commit cherry pick changes?

To change the commit message when cherry-picking, use “git cherry-pick” with the “-e” option. As illustrated in this example, your default editor will open and it will let you change the commit message. When you are satisfied with the edits, save your file and your commit message should be saved successfully.

Does cherry pick move the commit?

git cherry pick can also be passed some execution options. The --no-commit option will execute the cherry pick but instead of making a new commit it will move the contents of the target commit into the working directory of the current branch.

Does cherry picking create a new commit hash?

There's no need to redo the same changes in a different branch when you can just copy the same commits to the other branch. Please note that cherry-picking commits will create a fresh commit with a new hash in the other branch, so please don't be confused if you see a different commit hash.

What happens when you cherry pick a merge commit?

This command is also beneficial for undoing changes related to the past development done by the team. For instance, if we accidentally made a commit to the wrong branch, we will shift to the correct branch and cherry-pick the commit to the right place where it should belong in a new branch of the repository.


1 Answers

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.

like image 198
mwolfe02 Avatar answered Nov 02 '22 07:11

mwolfe02