Cherry-pick — Apply Specific Commits
Advancedgit cherry-pick applies the changes from one or more specific commits onto your current branch, creating new commits with the same diff but different hashes.
Overview
Cherry-pick is the targeted alternative to merging or rebasing an entire branch. It is most commonly used for backporting a hotfix to a maintenance branch: you fix the bug on main, then cherry-pick just that commit to release/v2.1. It is also useful for rescuing work from a dead branch, or applying one good commit from a colleague's branch without merging everything else. Like rebase, cherry-pick creates new commits (new hashes) with the same changes.
Cherry-picking Commits
Specify one or more commits by hash. Cherry-pick replays the diff of that commit onto HEAD.
// Scenario: hotfix on main, need to backport to release branch
//
// main: A ── B ── C ── fix ── D
// release/v2: A ── B ── C ──────────────
//
// We want "fix" on release/v2 WITHOUT D
# Find the commit hash of the fix
git log --oneline main
# f2a1c9b hotfix: prevent null pointer in checkout
# 3d9f2e0 Add new feature X
# a3f8c12 Add feature Y
# Switch to release branch and cherry-pick
git switch release/v2
git cherry-pick f2a1c9b
// After:
// main: A ── B ── C ── fix ── D
// release/v2: A ── B ── C ── fix'
// (fix' has the same changes as fix but a new hash)
# Cherry-pick a range of commits (oldest..newest)
git cherry-pick a3f8c12..f2a1c9b
# Cherry-pick without committing (stage only)
git cherry-pick --no-commit f2a1c9bHandling Conflicts in Cherry-pick
Like merge and rebase, cherry-pick can encounter conflicts. Resolve them the same way, then continue.
# Conflict during cherry-pick — similar to merge conflict
git cherry-pick f2a1c9b
# error: could not apply f2a1c9b... hotfix
# CONFLICT (content): Merge conflict in src/checkout.js
# 1. Edit the file to resolve the conflict
# 2. Stage the resolved file
git add src/checkout.js
# 3. Continue the cherry-pick
git cherry-pick --continue
# Or abort and return to pre-cherry-pick state
git cherry-pick --abort
# Cherry-pick a merge commit (must specify mainline parent)
git cherry-pick -m 1 <merge-commit-hash>Key Points to Remember
- 1cherry-pick applies the diff of a specific commit onto your current branch as a new commit
- 2Most common use: backporting a hotfix from main to a release/maintenance branch
- 3Cherry-pick creates a new commit with a new hash — the original commit is unchanged
- 4git cherry-pick --no-commit stages the changes without committing — useful for combining multiple cherry-picks
- 5Overusing cherry-pick leads to duplicate commits and confusing history — prefer merge/rebase when possible
Interview Questions
Sign in to ask AriaWhat is git cherry-pick and when would you use it?
What happens to the hash of a cherry-picked commit?
How would you backport a bug fix from main to an older release branch?
Ask Aria about Cherry-pick — Apply Specific Commits
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.