AlgoMaster Logo

Cherry-Pick Basics

Last Updated: January 3, 2026

5 min read

When working on a team project, you often find that some features or fixes are ready for production while others are still in progress.

Imagine you need to extract just one specific commit from a feature branch to apply to the main branch without merging everything else. This is where cherry-picking comes into play. It allows you to selectively apply commits from one branch to another, making it a powerful tool in your Git workflow.

This chapter delves into the basics of cherry-picking, covering how it works, why it's useful, and what to keep in mind when using it.

Understanding Cherry-Picking

At its core, cherry-picking is the act of selecting specific commits from one branch and applying them to another. This contrasts with traditional merging, where the entire history of changes from one branch is combined with another. With cherry-picking, you have the flexibility to choose only what you need.

How Cherry-Picking Works

When you cherry-pick a commit, Git creates a new commit on your current branch that contains the changes from the commit you selected. This new commit has a different hash because it’s effectively a copy of the original changes, but it will have a new parent commit—the current commit of the branch you’re on.

To visualize this, consider the following commit history:

If you decide to cherry-pick commit C from the feature branch while on the main branch, the history will look like this after the cherry-pick:

Here, C' is the new commit created on the main branch, containing the changes from C.

When to Use Cherry-Picking

Cherry-picking is particularly useful in several scenarios:

  • Hotfixes: If a bug is fixed in a feature branch and needs to be applied to the main branch immediately, cherry-picking that specific commit allows for quick action without waiting for the entire feature to be merged.
  • Selective Updates: Sometimes, you may want to apply a commit that is beneficial on a different branch without merging all changes. This is common when working with multiple release branches.
  • Experimentation: During development, you may find certain changes that you want to test on another branch. Cherry-picking allows you to do this without disrupting the original branch.

Practical Example

Let’s illustrate cherry-picking with a practical example. Suppose you have the following scenario where you have a feature branch that contains multiple commits, but only one commit needs to be applied to the main branch.

Check out the main branch:

Identify the commit to cherry-pick:

Use git log or git reflog to find the commit hash of the commit you want. Let's say the commit hash is abc1234.

Cherry-pick the commit:

Now, the changes from that specific commit are applied to your main branch. If the commit contains changes that introduce conflicts, Git will notify you, and you will have to resolve them before completing the cherry-pick.

Potential Pitfalls

While cherry-picking is powerful, it’s essential to be aware of some pitfalls:

  • Duplicate Commits: If you cherry-pick the same commit into multiple branches, you might end up with multiple identical commits across branches. This can lead to confusion in the commit history.
  • Conflicts: Cherry-picking can result in conflicts, especially if the selected commit modifies files that have also been changed in the target branch since the commits diverged. Understanding how to resolve these conflicts is crucial.
  • Loss of Context: When cherry-picking, you might lose the context or the reason behind certain changes if you do not document why a commit was cherry-picked. Always consider adding a message to the new commit to maintain clarity.

Best Practices for Cherry-Picking

To make the most of cherry-picking, consider the following best practices:

  • Use Descriptive Commit Messages: When creating the new commit through cherry-picking, you can use the -x option to include a reference to the original commit. This preserves the history and context.
  • Keep a Clean History: If you find yourself frequently cherry-picking from one branch to another, it might indicate a need to reevaluate your branching strategy. Consider whether merging or rebasing might better suit your workflow.
  • Test After Cherry-Picking: Always run your tests after cherry-picking to ensure that the changes integrate well with the target branch. This prevents introducing bugs into your main codebase.
  • Document Your Changes: Maintain clear documentation of why commits were cherry-picked, especially if you’re working in a team environment. This helps others understand the reasoning behind changes.

Now that you understand the fundamentals of cherry-picking, you’re ready to explore the mechanics of using the git cherry-pick command.

In the next chapter, we will look at its various options and how to effectively leverage them in your workflows. This will empower you to make the most out of cherry-picking and handle any situation that arises.