AlgoMaster Logo

git cherry pick

9 min readUpdated December 6, 2025
Listen to this chapter
Unlock Audio

When you need to bring specific changes from one branch into another, git cherry-pick is an incredibly useful command. It allows you to apply the changes introduced by a specific commit onto another branch without merging or rebasing.

This targeted approach can help maintain a clean history and allows for more precise control over what changes are integrated into a branch.

What is git cherry-pick?

At its core, git cherry-pick applies the changes from a specific commit to your current branch. Unlike merging, which incorporates all changes from one branch, cherry-picking focuses on individual commits.

This can be particularly advantageous when you want to take a bug fix or feature from a development branch and apply it to your production branch, without merging in all other changes that may not be ready for release.

When you run a cherry-pick, Git creates a new commit on your current branch that has the same changes as the original commit, but with a new commit hash. This new commit contains the same modifications, but Git treats it as a separate entity in the commit history.

How to Use git cherry-pick

The basic usage of git cherry-pick is simple. You need to specify the commit hash of the changes you want to apply. Here’s a step-by-step breakdown of how to do it:

  1. Identify the commit: Use git log to find the commit hash you wish to cherry-pick.
  2. Switch to the target branch: Ensure you are on the branch where you want the changes to be applied.
  3. Run the cherry-pick command: Execute git cherry-pick <commit-hash>.

Example Scenario

Imagine you have a feature branch where you fixed a critical bug and you want to apply that fix to your main branch.

After running these commands, Git creates a new commit on the main branch that reflects the changes from the abcdef1 commit of the feature branch.

Cherry-Picking Multiple Commits

Sometimes you may want to cherry-pick multiple commits at once. You can do this by specifying a commit range or listing out multiple commit hashes. There are two common approaches:

1. Using a commit range

You can cherry-pick a series of consecutive commits by specifying the start and end commit hashes.

This command will cherry-pick all commits from A to B, inclusive. The ^ indicates the parent commit of A, meaning you start from the commit just after it.

2. Specifying multiple commits

You can also cherry-pick non-consecutive commits by listing them out:

In this case, A, B, and C can be any commits, regardless of their order in the commit history.

Cherry-Pick with Options

The foundational command git cherry-pick has a variety of options that can modify its behavior.

-n or --no-commit

This option allows you to apply changes from the specified commit(s) to your working directory without immediately creating a new commit.

Using -n is particularly helpful when you want to review the changes or make additional modifications before committing.

Example

Suppose you want to cherry-pick a commit but require some adjustments before committing:

The above command applies the changes from commit abc1234 but allows you to tweak them before finalizing.

-x Option

The -x option adds a line to the commit message indicating the original commit from which changes were cherry-picked. This is useful for maintaining a clear history.

Example

When cherry-picking a commit with the -x option, you can run:

This will create a new commit with a message like:

This small addition can save a lot of confusion later, particularly in large projects with numerous contributors.

--strategy and Merge Strategies

When you cherry-pick commits, Git uses a merge strategy to apply changes. The --strategy option allows you to specify which merge strategy to use when applying the commit.

Common Strategies

  • recursive: This is the default strategy. It tries to find a common ancestor and performs a three-way merge.
  • ours: This strategy keeps the changes from the current branch and ignores changes from the cherry-picked commit where conflicts occur.
  • theirs: This is the opposite of ours; it takes changes from the cherry-picked commit during conflicts.

Example

Suppose you anticipate conflicts and wish to ignore the changes from the cherry-picked commit:

This command will apply the changes from abc1234, but whenever there is a conflict, it will favor the current branch's changes.

-e or --edit

The -e option opens the default text editor to allow you to modify the commit message after cherry-picking.

Example

To cherry-pick a commit and edit the message, you would run:

This command applies the changes and then opens the message for editing. This is helpful when you want to tailor the message to fit the context of your current branch.

Best Practices for Cherry-Picking

While git cherry-pick is powerful, it’s essential to use it judiciously. Here are some best practices to keep in mind:

  • Use Cherry-Pick for Isolated Changes: Cherry-picking is best for small, isolated changes like bug fixes or specific features that don’t require context from other commits.
  • Document Your Changes: When cherry-picking, consider adding a note in the commit message indicating where the changes came from. This will help future developers understand the history.
  • Avoid Overusing Cherry-Pick: Relying heavily on cherry-picking can lead to a complex commit history. If you find yourself cherry-picking multiple commits frequently, it might be worth reconsidering your branching strategy.
  • Test After Cherry-Picking: Always run tests after cherry-picking to ensure that the changes apply cleanly and do not introduce new bugs.

Use Cases for Cherry-Picking

Cherry-picking can be incredibly useful in real-world development scenarios. Here are some common use cases:

  • Hotfixes: If a critical bug is fixed in a feature branch and needs to be applied to production immediately, cherry-picking is an excellent way to merge that specific change.
  • Feature Deployment: When certain features are ready to be deployed but others are not, cherry-picking allows you to selectively pull in the necessary commits.
  • Experimentation: If you’re experimenting with features in a branch, you can cherry-pick successful experiments into your main branch without bringing along other untested changes.

Consider the following example. You have a production branch that requires a hotfix for a bug identified in the feature branch. By cherry-picking the relevant commit, you can efficiently apply the fix without disrupting ongoing development.

Now that you understand cherry-pick and the various options available for cherry-picking in Git, you're prepared to handle any complexities that may arise.

In the next chapter, we will explore cherry-pick conflicts, discussing how to resolve them effectively and keep your project moving forward. Get ready to learn how to turn potential setbacks into opportunities for clarity and better collaboration!