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: 18 min read author: Geek Workbench 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

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-off Analysis

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 Considerations

  • 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

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 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

Interview Questions

1. 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.

2. 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.

3. 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.

4. 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.

5. What is the difference between --no-commit and --edit flags?

--no-commit (or -n) applies the changes and stages them but does not create a commit, letting you review or modify before committing manually. --edit (or -e) creates the commit but opens your editor to modify the commit message before saving. Use -n for reviewing changes; use -e for editing the message of an already-created commit.

6. How do you resolve conflicts during cherry-pick?

When conflicts occur, Git marks them in the affected files. Resolve each file manually, then run git add <resolved-file> for each resolved file. Once all conflicts are staged, run git cherry-pick --continue to complete the operation. To abort and return to the pre-cherry-pick state, use git cherry-pick --abort.

7. What happens to the author information when you cherry-pick a commit?

The author information is preserved — the original author name, email, and timestamp are retained in the cherry-picked commit. However, the committer timestamp and SHA change because you (or another committer) are creating a new commit on the target branch. Use --signoff if you need to add your own attribution alongside the original author.

8. When should you use git revert instead of git cherry-pick?

Use git revert when you need to undo a commit's changes safely without removing it from history. Revert creates a new commit that applies the inverse of the target commit, making it safe for shared branches. Use cherry-pick when you want to apply specific changes to another branch (not undo them), such as backporting a fix. Revert is the safer choice for undoing; cherry-pick is for duplicating.

9. What happens if you cherry-pick a commit that modifies code already changed on the target branch?

You'll likely encounter merge conflicts. If the same lines were modified differently in each branch, Git cannot automatically resolve the difference. You must manually edit the conflicting sections, then stage and continue the cherry-pick. Even if there are no conflicts, the resulting code may have logical bugs if the context assumptions differ.

10. How does cherry-pick handle child commits if you only cherry-pick the parent?

Cherry-picking a single commit does not automatically bring its children. Each commit must be cherry-picked individually. If commit B depends on changes from commit A (its parent), cherry-picking only B will likely cause conflicts or produce incorrect results because B's changes assume A's changes are present. Always review dependencies before cherry-picking.

11. Can you use cherry-pick to move a tag? What happens?

Tags are references that point to specific commits. Cherry-picking a tag's commit creates a new commit with a different SHA, but the tag still points to the original commit. To "move" a tag, you must delete the old tag and create a new one pointing to the new commit: git tag -d old-tag && git tag old-tag new-sha. Note that this changes the tag's meaning for anyone who already pulled it.

12. What is the difference between cherry-picking to a branch versus cherry-picking to a tag?

Cherry-picking to a branch creates a new commit on that branch's tip. Cherry-picking to a tag is not directly possible — tags are static references. If you check out a tag (creating a detached HEAD), cherry-picking there creates an orphaned commit not reachable from any branch. You would need to explicitly create a branch from the tag first to preserve the cherry-picked work.

13. How does cherry-pick interact with submodules?

Cherry-pick preserves submodule references as long as the submodule commits exist. When you cherry-pick a commit that changes a submodule pointer, the new commit references the same submodule commit (or the nearest available one if the original is not local). If the submodule commit is missing, Git will report a submodule reference error until the submodule is updated.

14. What does git cherry-pick --signoff do that -x does not?

--signoff adds a trailer line indicating who performed the cherry-pick: Signed-off-by: Your Name <your@email>. This is used for license compliance (DCO - Developer Certificate of Origin) and shows who applied the change. -x only records the original commit SHA, not the identity of who cherry-picked it. Use both flags together for complete audit trails.

15. How can you preview what a cherry-pick will do before applying it?

Use git show <sha> to see the diff of the commit you intend to cherry-pick. You can also use git cherry-pick -n <sha> to apply changes without committing, then review the working tree with git diff. Once satisfied, commit manually with git commit. This gives you full control over the resulting commit message and lets you verify the changes.

16. Why might a cherry-pick succeed but the changes appear wrong in the target branch?

This happens when the cherry-picked commit depends on context that differs between branches — other commits, configuration files, API versions, or dependent code. The diff applies cleanly, but the change doesn't make sense in the new context. Always review the full diff with git show before cherry-picking, and run integration tests after merging to the target branch.

17. What is the --allow-empty flag in cherry-pick?

By default, cherry-pick skips commits that would produce no changes in the target branch. The --allow-empty flag forces creation of a commit even if the diff is empty. This is useful when the commit message contains important information (like a bug ID or change rationale) that needs to be preserved in the target branch's history.

18. How do you cherry-pick a range of commits excluding the start commit?

Use the range syntax with a caret on the start: git cherry-pick abc1234^..def5678 includes both commits. To exclude the first commit, start from the second one directly: git cherry-pick abc1235 def5678 (where abc1235 is the commit after abc1234). Alternatively, use git rebase to reorder and filter commits before cherry-picking.

19. What happens to stash entries when you cherry-pick?

Stash entries are independent of cherry-pick operations. Cherry-picking does not modify or delete stashes. However, if you have stashed changes and then cherry-pick commits that modify the same files, you may encounter conflicts when you later unstash. It's recommended to resolve all stash entries before performing cherry-picks to avoid confusion.

20. How do you atomic cherry-pick multiple commits to ensure they all succeed or fail together?

Cherry-picking multiple commits with git cherry-pick commit1 commit2 commit3 is not atomic by default — if one fails, previous commits in the sequence are already applied. For atomic behavior, use git cherry-pick --abort to restore the original state if any commit fails, or wrap the operation in a transaction script that checks the exit code and aborts if any cherry-pick fails, then retry or report the failure.

Further Reading

Additional Resources

CommandUse Case
git log --onelineFind commit SHAs to cherry-pick
git show <sha>Preview commit changes before applying
git reflogRecover from mistakes or find dropped commits
git revert <sha>Create inverse of a commit (safe alternative to cherry-pick for undoing)
git stashTemporarily store changes for later use

Advanced Topics to Explore

  • Cherry-pick vs. revert — when to use inverse commits instead of cherry-picking
  • git rerere — reuse recorded resolutions to handle repeated conflicts
  • Interactive rebase — clean up commits before cherry-picking to reduce conflicts
  • Bisect with cherry-pick — reproduce bugs across branches for testing

Conclusion

Cherry-pick is the surgical tool in Git’s toolkit — use it when you need exactly one change from another branch without merging everything. It’s invaluable for hotfixes, backporting bug fixes, and selective feature porting. Just be mindful that overuse can create confusing duplicate histories across branches.

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