Git Cherry-Pick: Selectively Applying Commits

Master git cherry-pick to selectively apply commits between branches. Learn use cases, pitfalls, and best practices for targeted commit transplantation.

published: reading time: 11 min read updated: March 31, 2026

Introduction

Cherry-picking is Git’s surgical tool for commit management. Instead of merging an entire branch or rebasing a series of commits, cherry-pick lets you select individual commits and apply them to a different branch. It’s the equivalent of saying “I want exactly this change, nothing else.”

This precision makes cherry-pick invaluable for specific scenarios — backporting bug fixes, extracting useful work from experimental branches, and applying hotfixes across multiple release lines. But it’s also one of the most misused Git commands, leading to duplicate commits, confusing histories, and merge conflicts.

Understanding when to cherry-pick and when to use other tools is the difference between a targeted fix and a historical mess. This guide covers the mechanics, the use cases, and the pitfalls.

When to Use / When Not to Use

When to Use Cherry-Pick

  • Backporting bug fixes — apply a fix from main to a maintenance release branch
  • Extracting useful commits — salvage good work from an abandoned experimental branch
  • Hotfix distribution — apply the same fix across multiple release branches
  • Correcting mistakes — apply a commit you forgot to include in a previous merge
  • Selective feature adoption — pull one specific feature from a large branch

When Not to Use Cherry-Pick

  • Regular feature integration — use merge or rebase instead
  • Large sets of commits — cherry-picking many commits creates duplicate history
  • When you need the full branch context — cherry-pick loses branch relationships
  • As a substitute for proper branching — don’t cherry-pick to avoid learning merge/rebase
  • Commits with complex dependencies — if a commit depends on others, cherry-pick may break

Core Concepts

Cherry-pick takes a commit from one branch and creates a new commit with the same changes on your current branch. The new commit has a different SHA but the same diff.


Before cherry-pick:
main:     A ── B ── C ── D
           \
feature:      E ── F ── G

After cherry-picking F onto main:
main:     A ── B ── C ── D ── F'
           \
feature:      E ── F ── G

F’ has the same changes as F but a different SHA and a different parent (D instead of E).


graph TD
    Start["Identify commit to cherry-pick"] --> Find["git log --oneline\nor git reflog"]
    Find --> Switch["git switch target-branch"]
    Switch --> Apply["git cherry-pick <sha>"]
    Apply --> Conflict{"Conflict?"}
    Conflict -->|Yes| Resolve["Resolve, git add,\ngit cherry-pick --continue"]
    Conflict -->|No| Done["New commit created\non current branch"]
    Resolve --> Done

Architecture or Flow Diagram


flowchart TD
    A["Source Branch:\ncommit F (bug fix)"] --> Identify["Identify commit SHA"]
    B["Target Branch:\nrelease/v1.0"] --> Switch["git switch release/v1.0"]
    Identify --> Cherry["git cherry-pick F"]
    Switch --> Cherry
    Cherry --> Result{"Result"}
    Result -->|Clean| NewCommit["New commit F'\nSame changes, new SHA"]
    Result -->|Conflict| ManualResolve["Resolve conflicts\nmanually"]
    ManualResolve --> NewCommit
    NewCommit --> Verify["Run tests\nVerify fix works"]
    Verify --> Push["git push origin release/v1.0"]

Step-by-Step Guide / Deep Dive

Basic Cherry-Pick


# Cherry-pick a single commit
git cherry-pick abc1234

# Cherry-pick multiple commits (in order)
git cherry-pick abc1234 def5678 ghi9012

# Cherry-pick a range of commits (inclusive)
git cherry-pick abc1234..def5678

# Cherry-pick a range including the start commit
git cherry-pick abc1234^..def5678

Cherry-Pick Options


# Apply changes but don't commit (stage only)
git cherry-pick --no-commit abc1234

# Edit the commit message before committing
git cherry-pick --edit abc1234

# Preserve the original commit message (add reference)
git cherry-pick -x abc1234
# Appends: (cherry picked from commit abc1234...)

# Apply without auto-committing (for review)
git cherry-pick -n abc1234

# Abort an in-progress cherry-pick
git cherry-pick --abort

# Continue after resolving conflicts
git cherry-pick --continue

Finding Commits to Cherry-Pick


# Find commits by author
git log --author="username" --oneline

# Find commits that touched a specific file
git log --oneline -- path/to/file

# Find commits containing a keyword
git log --grep="bug fix" --oneline

# Find commits between two dates
git log --after="2026-03-01" --before="2026-03-31" --oneline

# Show the diff of a commit before cherry-picking
git show abc1234

Cherry-Picking Across Repositories


# Add the source repository as a remote
git remote add source-repo https://github.com/other/repo.git
git fetch source-repo

# Cherry-pick from the fetched remote
git cherry-pick source-repo/main~3

# Clean up
git remote remove source-repo

Production Failure Scenarios + Mitigations

ScenarioImpactMitigation
Cherry-picking dependent commitsMissing dependencies cause breakageReview commit dependencies; cherry-pick in order
Duplicate commits in historySame change appears with different SHAsUse -x flag to track origin; document in PR
Cherry-pick conflicts on old branchesCode has diverged significantlyTest thoroughly; consider backporting manually
Losing original commit attributionAuthor information may be lostUse --signoff and -x to preserve attribution
Cherry-picking merge commitsCreates unexpected resultsUse -m 1 to specify which parent to use

Cherry-Picking Merge Commits


# Cherry-pick a merge commit (specify which parent to diff against)
git cherry-pick -m 1 <merge-commit-sha>
# -m 1: diff against first parent (usually the branch you were on)
# -m 2: diff against second parent (the merged branch)

Trade-offs

ApproachProsCons
Cherry-pickTargeted, precise, flexibleCreates duplicate commits, loses context
MergePreserves relationships, single source of truthBrings entire branch, may include unwanted changes
RebaseClean history, linearRewrites history, not for shared branches
Manual backportFull control, can adapt changesTime-consuming, error-prone
Cherry-pick -xTracks origin, auditableStill creates duplicate commits
Cherry-pick —no-commitReview before committingExtra step, easy to forget to commit

Implementation Snippets


# Backport a bug fix to release branch
git switch release/v1.2
git cherry-pick -x abc1234  # -x adds origin reference
git push origin release/v1.2

# Cherry-pick a range of commits
git switch feature-partial
git cherry-pick main~5..main~2  # commits between these points

# Cherry-pick and edit message
git cherry-pick --edit def5678

# Cherry-pick without committing (review first)
git cherry-pick --no-commit ghi9012
git diff --cached  # review the staged changes
git commit -m "Backport: fix authentication timeout"

# Batch cherry-pick for hotfix distribution
for branch in release/v1.0 release/v1.1 release/v1.2; do
    git switch $branch
    git cherry-pick -x abc1234
    git push origin $branch
done

Observability Checklist

  • Logs: Record cherry-pick operations with original commit references
  • Metrics: Track cherry-pick frequency (high frequency may indicate branching issues)
  • Alerts: Alert on cherry-picks to production branches without review
  • Traces: Link cherry-picked commits to original commits via -x flag
  • Dashboards: Display backport success rate and conflict frequency

Security/Compliance Notes

  • Cherry-picked commits should reference the original commit for audit trails
  • Use -x flag to automatically append the original commit SHA
  • Verify that cherry-picked security fixes don’t introduce regressions
  • Document cherry-pick operations in release notes
  • Ensure cherry-picked commits pass the same CI checks as regular commits

Common Pitfalls / Anti-Patterns

  • Cherry-picking without understanding dependencies — commits often depend on earlier work
  • Creating duplicate history — cherry-picking merged commits creates confusion about what’s actually in a branch
  • Forgetting to test — cherry-picked changes may behave differently in a different context
  • Cherry-picking merge commits without -m — produces unexpected results
  • Using cherry-pick as a regular workflow — it should be the exception, not the rule
  • Losing attribution — always use -x or --signoff to preserve the original author

Quick Recap Checklist

  • Cherry-pick applies individual commits to a different branch
  • Use git cherry-pick <sha> for single commits
  • Use -x to track the original commit source
  • Use --no-commit to review changes before committing
  • Use -m 1 when cherry-picking merge commits
  • Resolve conflicts with git cherry-pick --continue
  • Abort with git cherry-pick --abort
  • Always test cherry-picked changes in their new context

Interview Q&A

What is the difference between git cherry-pick and git merge?

git merge integrates all changes from one branch into another, creating a merge commit that records the relationship. git cherry-pick applies specific individual commits to the current branch, creating new commits with different SHAs. Merge preserves history; cherry-pick duplicates it.

What does the -x flag do in git cherry-pick?

The -x flag appends a line to the commit message indicating the original commit SHA: (cherry picked from commit abc1234...). This creates an audit trail linking the cherry-picked commit to its source, which is invaluable for tracking where changes originated.

How do you cherry-pick a merge commit?

Use the -m flag to specify which parent to diff against: git cherry-pick -m 1 <merge-sha>. -m 1 uses the first parent (the branch you were on when merging), -m 2 uses the second parent (the merged branch). Without -m, Git doesn't know which parent to use as the base.

Why is cherry-picking many commits from a branch problematic?

Cherry-picking creates new commits with different SHAs. If you later merge the original branch, Git doesn't recognize that the changes are already present, leading to duplicate changes or conflicts. For multiple commits, prefer merge or rebase to maintain the relationship between commits.

Architecture: How Cherry-Pick Creates New Commits


graph TD
    subgraph "Source Branch: feature"
        E["commit E"] --> F["commit F (bug fix)\nSHA: abc1234"]
        F --> G["commit G"]
    end

    subgraph "Target Branch: release/v1.0"
        D["commit D"] --> NewF["commit F'\nSHA: xyz7890\nSame diff, different SHA\nParent: D instead of E"]
    end

    F -. "git cherry-pick F" .-> NewF
    F -. "Same changes" .-> NewF

    classDef commit fill:#16213e,color:#00fff9
    class E,F,G,D,NewF commit

Cherry-pick computes the diff introduced by the source commit, then applies that diff to the target branch and creates a brand new commit with the target branch as its parent. The new commit has a different SHA, different parent, and different commit date (though author info is preserved).

Production Failure: Cherry-Picking Across Diverged Branches

Scenario: A developer cherry-picks a bug fix from main (which uses a new API version) onto release/v1.0 (which still uses the old API). The cherry-pick applies cleanly because the changed lines don’t overlap, but the fix depends on the new API that doesn’t exist in the release branch.

Impact: The code compiles and tests pass in isolation, but the fix silently fails in production because the API call returns unexpected data. The bug appears “fixed” but the underlying issue persists.

Mitigation:

  • Always review the full diff before cherry-picking, not just the conflict status
  • Check for hidden dependencies — does the commit rely on changes in other commits?
  • Run integration tests on the target branch after cherry-picking
  • Use git show <sha> to understand the full context before cherry-picking
  • Consider manual backporting instead of cherry-pick for significantly diverged branches

# Review the full diff before cherry-picking
git show abc1234

# Check what files the commit touches
git diff-tree --no-commit-id --name-only -r abc1234

# Check if the commit depends on earlier commits
git log --oneline abc1234~5..abc1234

# After cherry-pick, verify the change makes sense in context
git diff HEAD~1  # compare new commit with its parent

Trade-offs: Backporting Hotfixes

ApproachBest ForProsCons
Cherry-pickSingle, self-contained commitsFast, precise, preserves attribution with -xCreates duplicate commits, may miss dependencies
MergeEntire feature branchesPreserves relationships, single source of truthBrings all changes, may include unwanted work
RebaseKeeping branches currentClean linear historyRewrites history, not for shared branches
Manual backportSignificantly diverged branchesFull control, can adapt changes to target contextTime-consuming, error-prone, loses attribution
Cherry-pick -xAuditable backportsTracks origin automaticallyStill creates duplicate commits

Summary Checklist

  • Cherry-pick creates a new commit with the same changes but different SHA
  • Always review the full diff with git show <sha> before cherry-picking
  • Use -x flag to track the original commit source for audit trails
  • Check for hidden dependencies — commits often rely on earlier work
  • Run integration tests on the target branch after cherry-picking
  • Use --no-commit to review changes before committing
  • Use -m 1 when cherry-picking merge commits
  • Consider manual backporting for significantly diverged branches
  • Cherry-pick should be the exception, not the regular workflow

Resources

Category

Related Posts

Master git add: Selective Staging, Patch Mode, and Staging Strategies

Master git add including selective staging, interactive mode, patch mode, and staging strategies for clean atomic commits in version control.

#git #staging #git-add

Git Branch Basics: Creating, Switching, Listing, and Deleting Branches

Master the fundamentals of Git branching — creating, switching, listing, and deleting branches. Learn the core commands that enable parallel development workflows.

#git #version-control #branching

Rebase vs Merge: When to Use Each in Git

Decision framework for choosing between git rebase and git merge. Understand trade-offs, team conventions, history implications, and production best practices.

#git #version-control #rebase