Skip to content
ExponaBLOG
GIT & GITHUB FOR PROFESSIONAL DEVELOPERS · DAY 021 / 100

Git cherry-pick explained: practical tips and trade-offs

Copy specific commits between branches.

Expona Admin8 min read

Introduction

You've made a critical bug fix on your main branch, but it also needs to go into your release branch immediately—without merging everything else that's in flight. Or maybe you want to pull a single feature commit from a colleague's branch into yours. Git cherry-pick explained: practical tips and trade-offs covers one of the most powerful—and misused—tools in version control. Cherry-pick lets you apply specific commits to your current branch, bypassing the normal merge workflow. When used wisely, it solves real problems in production environments. When overused, it creates duplicate commits and confusing history that haunts your team for months.

In this article, we'll explore cherry-pick from the ground up: when to use it, how to use it safely, and—most importantly—when to avoid it altogether.

Basic use: apply one commit to the current branch

The simplest cherry-pick operation copies a single commit from one branch to another. Imagine you're on your feature branch and a teammate merged a utility function to main that you need right now. Instead of waiting for main to be rebased into your branch, you can grab that commit directly.

bash
git cherry-pick a1b2c3d

This command takes the commit with hash a1b2c3d and applies it on top of your current branch. Git creates a new commit with the same changes but a different hash (since the parent commit is different). Your working directory updates to reflect the changes, and you can immediately test the fix or feature.

Let's walk through a realistic scenario. Your team is working on a payment processing overhaul, but you notice that the rate-limiting logic in the API gateway is broken. A senior engineer fixes it on main and commits it as 7f3e9c2. You need that fix in your feature branch without pulling all the other half-finished work from main.

bash
git log --oneline main | head -20
# Output:
# 7f3e9c2 Fix rate limiter bucket refill logic
# 4d2a1b8 Add observability metrics (incomplete)
# 9c1e3f5 Refactor cache layer (WIP)
# ...

git checkout feature/payment-processing
git cherry-pick 7f3e9c2

# Output:
# [feature/payment-processing abc1234] Fix rate limiter bucket refill logic
# 1 file changed, 8 insertions(+), 2 deletions(-)

Now your feature branch has the rate-limiter fix without the incomplete work. The commit message is preserved, and your history is clean and linear. This works particularly well for small, self-contained commits that don't depend on a chain of other changes.

One important note: if the commit you're cherry-picking depends on changes from other commits (perhaps it imports a new utility that was added in a previous commit), the cherry-pick will either fail with a conflict or succeed but leave your code broken. Always verify that the commit is truly independent before cherry-picking.

Hotfixes: pick a fix from main into a release branch

Cherry-pick shines in hotfix scenarios. You're running version 2.5 in production, but a critical security flaw is discovered. The fix is merged to main, which is already five commits ahead preparing for version 2.6. You need that security patch in the 2.5 release branch without bringing in untested 2.6 features.

Let's say the security fix is commit 9e2f1c3 on main.

bash
git checkout release/2.5
git log --oneline -5
# Output:
# f4a3e1b Version 2.5.0 release
# 3c2b1a9 Performance optimization for database queries
# ...

git cherry-pick 9e2f1c3

# Output:
# [release/2.5 d7c9e1f] Fix SQL injection vulnerability in user input handler
# 1 file changed, 12 insertions(+), 3 deletions(-)

You can now build and test version 2.5.1 with only the security fix. This is safer than pulling a long chain of merged commits, and it keeps your release process predictable.

However, best practice here is to apply the fix to main first, then cherry-pick it backward to release branches. Never work on a release branch and then try to merge those changes forward—you'll create a tangled mess of duplicate commits and conflicts. The general rule is: cherry-pick down the hierarchy (from newer to older versions), never up.

After the hotfix is released as 2.5.1, make sure the same fix stays in main for version 2.6. You may need to apply it again if it was subsequently changed or removed.

Ranges: copy a series of commits

Sometimes you need to pick multiple sequential commits, not just one. Git lets you specify a range of commits using the ^..B syntax (or the newer A..B syntax, which excludes A).

bash
git cherry-pick A^..B

This applies every commit from A (inclusive) through B (inclusive) to your current branch. Let's say you've been working on a feature branch and you've made three solid commits that are ready to go into a parallel feature branch.

bash
git log --oneline feature/payment-redesign | head -10
# Output:
# c1d2e3f Add payment method selector component
# b2c3d4e Implement new checkout flow
# a1b2c3d Create payment validation schema
# 9f8e7d6 Old commit from before the feature work
# ...

git checkout feature/recurring-billing
git cherry-pick a1b2c3d^..c1d2e3f

# Output:
# [feature/recurring-billing xyz1234] Create payment validation schema
# 1 file changed, 45 insertions(+), 3 deletions(-)
# [feature/recurring-billing uvw5678] Implement new checkout flow
# 2 files changed, 89 insertions(+), 5 deletions(-)
# [feature/recurring-billing rst9012] Add payment method selector component
# 1 file changed, 23 insertions(+), 1 deletion(-)

Each commit in the range is applied in order, creating three new commits on your current branch. If there are conflicts, cherry-pick will stop at the first one and ask you to resolve it before continuing.

One critical caveat: ranges work best when the commits are truly independent. If the later commits in the range depend on changes from the earlier ones, you're fine. But if they depend on commits outside the range, you'll get conflicts or broken code. Always review the commits in the range before applying them.

Use sparingly: why duplicate commits cause problems

Cherry-pick is powerful, but it's also dangerous when overused. Every time you cherry-pick a commit, you create a duplicate of the changes with a different hash. This breaks Git's ability to track commits as they move through your codebase.

Consider this scenario: you cherry-pick a commit from main to a release branch, then later merge the release branch back to main. Git sees two different commits with the same changes. You now have what's called a "duplicate commit" in your history. When you use git log --oneline, both commits appear separately. When you use git blame on a file, the line might show one commit hash or the other depending on which path through the history Git traversed. This confuses developers and breaks tools that rely on commit hashes being stable identifiers.

A better approach for most scenarios is a regular merge or rebase. If you cherry-pick a commit and later merge the source branch, Git's merge strategy will detect the duplicate and skip it—but your history still shows the unnecessary cherry-pick. Over time, these duplicates accumulate and make your repository harder to work with.

When cherry-pick is appropriate: applying a hotfix to multiple release branches, pulling a single fix into a feature branch when you can't merge the entire source branch, or recovering a lost commit that was accidentally reverted.

When to use merge or rebase instead: pulling new features from main into your branch, integrating a colleague's finished feature branch, or keeping your work in sync with the team's changes. These operations preserve the logical history and avoid duplicates.

Common mistakes

Cherry-picking without testing the dependency chain. A commit might compile and pass its original tests but break in a new context if it depends on changes from other commits. Always verify that the commit is truly self-contained, or run your full test suite after cherry-picking.

Cherry-picking and then merging the source branch. This creates duplicate commits in your history. If you've already cherry-picked, use git merge --no-ff on the source branch later, and Git will skip the duplicate commits (though the merge commit will still appear). Better yet, avoid this pattern by choosing merge or cherry-pick upfront, not both.

Using cherry-pick for regular feature integration. If you're constantly cherry-picking individual commits from main into your feature branch instead of doing a regular rebase or merge, your workflow is broken. This suggests your feature branch is out of sync and you need a different strategy. Consider rebasing on main instead.

Not documenting why you cherry-picked. If your team sees a cherry-picked commit in the history without context, they won't understand why you deviated from the normal workflow. Add a note to your pull request or commit message explaining that you cherry-picked a hotfix or recovery, not that it was a regular development choice.

Cherry-picking with uncommitted changes. Always commit or stash your work before cherry-picking. If there are conflicts, cherry-pick can leave your working directory in an inconsistent state, and you'll lose any uncommitted changes.

Key takeaways

  • Use git cherry-pick <commit> to apply a single commit to your current branch; use ranges like A^..B for multiple sequential commits.
  • Cherry-pick is ideal for hotfixes, pulling a single fix into an off-sync branch, and recovering lost commits—not for routine feature integration.
  • Every cherry-pick creates a new commit with a different hash. Duplicate commits in your history cause confusion and break traceability tools.
  • Avoid cherry-picking and then merging the source branch; choose one workflow or the other to keep your history clean.
  • Test cherry-picked commits thoroughly to verify they don't depend on changes from other commits.
  • Document in your PR or commit message why you used cherry-pick instead of a standard merge or rebase.
  • When in doubt, use merge or rebase instead. Cherry-pick should be the exception, not the rule.
ShareXLinkedInReddit

Found this useful? Support the writing.

Buy me a coffee
Written by
Expona Admin
Site administrator

Comments

Loading comments…

Leave a comment

Comments are reviewed before they appear. Be kind and stay on topic.

Related articles