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.

What is Git cherry pick ?

Cherry-picking in Git refers to selecting a specific commit (or commits) from one branch and applying it to another. For example, if you’ve fixed a bug in a feature branch and need the same fix in the main branch, cherry-picking lets you apply just that fix without merging the entire feature branch.

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 (Git cherry pick)

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.

git cherry pick
git cherry pick

Handling Conflicts: Git cherry pick

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:


Git Cherry pick Multiple Commits

Cherry-picking multiple commits is particularly useful when:

  1. You need to backport several fixes to a release branch.
  2. You want to incorporate multiple feature-specific changes into another branch.
  3. You’re extracting specific work from a feature branch to be shared elsewhere.

1. Specify Git cherry pick 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 : Git cherry pick range Multiple Commits

You can git cherry-pick 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. Git Cherry pick multiple 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 Git Cherry Pick 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:


Best Practices for Git Cherry Pick

  1. Keep Commits Atomic: Make sure each commit addresses a single logical change.
  2. Use Clear Commit Messages: Descriptive commit messages help identify relevant changes for cherry-picking.
  3. Limit History Divergence: Regularly sync branches to reduce conflicts.
  4. Document Cherry-Picks: Track cherry-picks in your workflow to avoid duplication

Step-by-Step Guide to Git cherry pick range of commits

Here is a detailed walkthrough of git cherry pick range of commits:

Identify the Commits

To cherry-pick a range (git cherry pick range), you first need to identify the commit hashes. Use the git log command to view the commit history.

This displays a concise list of commit hashes and their messages, making it easy to identify the range you want to cherry-pick. For example.

Checkout the Target Branch

Switch to the branch where you want to apply the commits. Use the git checkout or git switch command:

Run the Cherry-Pick Command

Use the git cherry-pick command with the range of commits:

For example:

Resolve Conflicts (if any)

If there are conflicts during the cherry-pick operation, Git will pause and display a message.


Conclusion

Git Cherry pick is an invaluable Git feature for managing code selectively across branches. While straightforward for single commits, it becomes even more powerful when applied to multiple commits. By mastering the techniques of cherry-picking ranges, non-consecutive commits, and resolving conflicts, developers can streamline workflows, maintain clean branch histories, and handle complex scenarios with confidence.

Whether you’re back-porting fixes, integrating selected features, or managing hotfixes, understanding git cherry pick equips you with the flexibility to adapt to real-world development challenges effectively.

Check out more blogs


FAQ’s

What does the ^ symbol mean in the git cherry pick range syntax?

The ^ symbol ensures that the <start-commit> itself is included in the range. Without it, the range would begin with the commit following <start-commit>

Can I cherry-pick a range without creating commits immediately?

Yes, use the --no-commit flag to stage the changes without committing.