Stage 2 · Tools
Branching & Merging
Cherry-Picking
Apply individual commits across branches without merging — when to cherry-pick and when to merge instead.
What Cherry-Pick Does
Cherry-pick applies a specific commit from one branch onto another branch. It creates a new commit with the same changes but a different SHA hash. The original commit remains untouched on its source branch.
Think of cherry-picking as transplanting a single commit. You reach into one branch, grab a specific commit, and graft it onto your current branch.
The cherry-picked commit gets a new SHA hash because its parent is different. This is not a reference to the original commit — it is a new commit with the same changes.
Cherry-Pick a Single Commit
# Switch to the target branch
git switch release/2.0
# Cherry-pick a commit by its hash
git cherry-pick a1b2c3d
# View the result
git log --oneline -3
# e9f0g1h (HEAD -> release/2.0) Fix critical auth bug
# b2c3d4e (release/2.0) Prepare release 2.0
# 1a2b3c4 Initial commitGit takes the changes from commit a1b2c3d and applies them as a new commit on release/2.0. The commit message is preserved by default.
# Cherry-pick a commit from a remote tracking branch
git cherry-pick origin/main~3
# Cherry-pick a specific commit from a feature branch
git cherry-pick origin/feature/api~0You can cherry-pick from any ref — branches, tags, or remotes. Use origin/branch~N to reference commits relative to the branch tip.
Cherry-Pick a Range of Commits
# Cherry-pick commits from A to B (exclusive of A)
git cherry-pick A..B
# Cherry-pick commits from A to B (inclusive of A)
git cherry-pick A^..B
# Cherry-pick multiple individual commits
git cherry-pick a1b2c3d d4e5f6g g7h8i9jThe range syntax A..B picks commits that are reachable from B but not from A. The ^ on A includes it in the range. You can also list multiple commit hashes separated by spaces.
Run git cherry-pick --no-commit <commit> to apply changes to your working tree without creating a commit. This lets you review and combine multiple cherry-picks into a single commit.
Cherry-Pick with Attribution
# Cherry-pick and add a reference to the original commit
git cherry-pick -x a1b2c3d
# The resulting commit message includes:
# (cherry picked from commit a1b2c3d...)The -x flag appends a line to the commit message referencing the original commit. This makes it easy to trace where the change came from. It is useful for release branches and backports.
The -x flag is especially valuable for compliance-sensitive projects where you need to trace every change back to its origin. It creates a clear paper trail from release branches to the original development.
Cherry-Pick vs Merge
| Aspect | Cherry-Pick | Merge |
|---|---|---|
| Scope | Single commit or small range | Entire branch history |
| Commit identity | New commit with new SHA | Original commits preserved |
| Traceability | Requires -x flag for attribution | Merge commits show source branch |
| History impact | Adds individual commits | Adds merge commit (or fast-forwards) |
| Conflict scope | Conflicts per commit | All conflicts resolved at once |
| Best for | Hotfixes, backports, selective changes | Integrating complete features |
Use cherry-pick when you need to move a specific, isolated change between branches. Use merge when you want to integrate the full state of one branch into another.
Resolving Conflicts During Cherry-Pick
# Cherry-pick that produces a conflict
git cherry-pick a1b2c3d
# CONFLICT (content): Merge conflict in config.ts
# error: could not apply a1b2c3d...
# hint: After resolving the conflict, mark the corrected path
# Check the conflicted files
git status
# Resolve by editing the file, then:
git add config.ts
git cherry-pick --continue
# Or skip this commit entirely
git cherry-pick --skip
# Or abort the entire cherry-pick
git cherry-pick --abortConflict resolution during cherry-pick works exactly like merge conflicts: edit the file, stage it, and continue. The --continue flag resumes the cherry-pick after resolution.
When cherry-picking multiple commits that might conflict, use --no-commit on each one. This stages all changes without committing, letting you resolve conflicts once and commit everything together.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.