Git cherry pick

git cherry pick is a powerful command in Git that allows you to apply changes from specific commits in one branch to another branch. This is particularly useful when you want to incorporate individual changes without merging entire branches.

git cherry pick
github

Basic Usage:

To apply a commit from another branch to your current branch, first ensure you’re on the target.

Then, execute the cherry-pick command with the commit hash


Common Options in git cherry pick

1. -e or -edit

  1. Allows you to edit the commit message before creating the new commit.
  2. Useful when you want to modify or add additional details to the commit message.

Example

2. -x

  1. Appends a line to the commit message indicating the original commit hash.
  2. This is helpful for tracking where the commit originated

Example

3. -n or –no-commit

Applies the changes from the commit(s) to the working directory without creating a new commit.

Useful if you want to review, modify, or combine changes before committing.

Example

4. -m <parent-number> or –mainline <parent-number>

  1. Used for cherry-picking merge commits.
  2. Specifies which parent of the merge commit should be considered as the mainline to replay changes.

Example

1 refers to the first parent of the merge commit.

This option is necessary for cherry-picking merge commits, as Git cannot automatically determine the base.

5. –cleanup=<mode>

Specifies how to clean up the commit message.

  1. default: Cleans up whitespace and comments.
  2. verbatim: Uses the commit message exactly as it is.
  3. whitespace: Removes unnecessary whitespace

Example

6. -rerere-autoupdate

Automatically updates the index with the result of resolved conflicts, using the recorded resolutions if possible.

Example

7. –no-gpg-sign

Disables GPG signing for the cherry-picked commit, even if GPG signing is enabled globally.

Example

8. –allow-empty-message

Allows cherry-picking of commits that have no commit message.

Example

9. –allow-empty

Allows cherry-picking of commits that don’t introduce any changes (empty commits).

10. -s or –signoff

Adds a “Signed-off-by” line at the end of the commit message.

This is commonly used for contributing to projects that follow the Developer Certificate of Origin (DCO)

Example

11. –strategy-option=<option>

Adds options for the specified merge strategy.

For example, to resolve conflicts by automatically favoring the current branch

12. –strategy=<strategy>

Specifies the merge strategy to use when applying changes.Common strategies:

  1. recursive (default): Merges changes recursively.
  2. ours: Keeps the current branch’s changes.
  3. theirs: Uses the incoming changes

13. –quite

Stops the cherry-pick process but leaves any changes made so far in the working directory.Use this if you want to pause the cherry-pick process without resetting everything.

Example:

14. –abort

Cancels the cherry-pick process and resets your branch to the state before the cherry-pick began.

Example:

15. –continue

Resumes the cherry-pick process after resolving conflicts.

Example:


Summary of Options

OptionDescription
-e / --editEdit the commit message before committing.
-xAppend the original commit hash to the message.
-n / --no-commitApply changes without committing.
-m <parent>Specify the parent for merge commits.
--continueResume after resolving conflicts.
--abortCancel the cherry-pick.
--quitStop the process but keep changes.
--strategy=<name>Specify the merge strategy.
--strategy-optionProvide additional options for the merge strategy.
-s / --signoffAdd a “Signed-off-by” line to the commit message.
--allow-emptyCherry-pick empty commits.
--allow-empty-messageCherry-pick commits without a message.
--no-gpg-signDisable GPG signing for the commit.
--rerere-autoupdateAutomatically resolve conflicts using previously recorded resolutions.
--cleanup=<mode>Clean up commit messages based on the specified mode.

Handling Conflicts:

If conflicts arise during a cherry-pick, Git will pause and allow you to resolve them. After resolving conflicts, you can continue the process with:

To abort the cherry-pick and return to the state before it was initiated:


Cherry-picking Multiple Commits

1. Specify Multiple Commit Hashes

You can list multiple commit hashes separated by spaces. For example:

Git will sequentially apply each of these commits to the current branch. If any conflicts arise, Git will pause the process for you to resolve the conflicts.

2. Use a Range of Commits

You can cherry-pick a range of commits using the .. syntax. For example:

In this case:

  • <start-commit-hash> is not included in the range.
  • <end-commit-hash> is included in the range.

To include both commits in the range, prepend ^ to the starting commit:

3. Cherry-picking Sequential Commits

If the commits you want to cherry-pick are sequential, you can use HEAD~n syntax or branch references:

Where:

  • A is the older commit (not included unless using A^).
  • B is the newer commit (always included).

Options for Cherry-picking Multiple Commits

Edit Commit Messages: To edit commit messages for each commit during the cherry-pick process:

Append Original Commit Info: To include a reference to the original commit in the new commits:

No Automatic Commit: To stage changes from all cherry-picked commits without committing them: