The git cherry-pick is a very useful command that takes changes from a specific commit and applies them to your current branch in a new commit.

1. Cherry-pick using Git commit hash

The easiest way to cherry-pick a commit is to use the “cherry-pick” command with the commit hash.

$ git cherry-pick <hash>

2. Cherry-pick from another branch

In order to pick commits from another branch, you need to list commits that were performed on this other branch using the “git log” command.

$ git log --oneline <branch>

And follow from Step 1.

$ git cherry-pick <hash>

3. Cherry-pick multiple Git commits

In some cases, you may want to cherry-pick multiple commits at once.

Luckily for you, this option is available since Git 1.7.2.

Since Git 1.7.2, you can cherry-pick a range of commits by using the dot notation.

$ git cherry-pick A..B

Note that using this command, the commit A will NOT be included into the cherry-pick.

In order to include the commit A, you can use this syntax

$ git cherry-pick A^..B

4. Cherry-pick with original commit reference

In some cases, you may want to keep a reference to the original commit when performing a cherry-pick.

When you are performing a regular cherry-pick, you will get a new commit hash but the commit message will be the same.

However, there is a way to append the origin of a cherry-pick to the commit message : by using the cherry-pick with the “-x” option.

$ git cherry-pick -x <commit>

5. Change commit message when cherry-picking

Cherry-picking commits is very useful in order to reuse some existing work.

However, in some branches, the commit may be useful for different reasons.

As a consequence, you may want to change the commit message when cherry-picking.

To change the commit message when cherry-picking, use “git cherry-pick” with the “-e” option.

$ git cherry-pick -e <hash>