Cherry Picking Commits
Cherry-pick lets you apply specific commits from one branch to another without merging the entire branch. It's like picking individual cherries from a tree.
What is Cherry-Pick?
Cherry-pick copies a commit to your current branch:
Before:
main: A ← B ← C
\
feature: D ← E ← F
After cherry-pick E onto main:
main: A ← B ← C ← E'
\
feature: D ← E ← F
Note: E' is a new commit with the same changes as E, but a different hash.
Basic Cherry-Pick
# Switch to target branch
git checkout main
# Cherry-pick a specific commit
git cherry-pick abc1234
This applies the changes from commit abc1234 to your current branch.
Cherry-Pick Options
Multiple Commits
# Pick multiple commits
git cherry-pick abc1234 def5678 789abcd
# Pick a range (excludes first commit)
git cherry-pick abc1234..def5678
# Pick a range (includes first commit)
git cherry-pick abc1234^..def5678
Without Committing
# Apply changes but don't commit
git cherry-pick --no-commit abc1234
# Now you can modify or combine with other changes
git commit -m "Custom message"
Preserve Original Authorship
# Keep original author (default)
git cherry-pick abc1234
# Sign off
git cherry-pick -s abc1234
Record Original Commit
# Add note about original commit
git cherry-pick -x abc1234
# Message: "Cherry-picked from commit abc1234"
When to Use Cherry-Pick
Scenario 1: Hotfix to Multiple Branches
# Fix is on develop
# abc1234 - Fix critical security bug
# Apply to production branch
git checkout production
git cherry-pick abc1234
# Apply to release branch
git checkout release/2.0
git cherry-pick abc1234
Scenario 2: Pull Specific Feature from Feature Branch
# feature branch has:
# abc1234 - Add validation (want this)
# def5678 - WIP: other stuff (don't want)
# 789abcd - Add tests (want this)
git checkout main
git cherry-pick abc1234 789abcd
Scenario 3: Recover Lost Work
# Found an orphaned commit in reflog
git reflog
# abc1234 HEAD@{10}: commit: Important work
# Apply it to current branch
git cherry-pick abc1234
Scenario 4: Backport to Older Version
# Fix exists in v3
# Need to apply to v2 maintenance branch
git checkout v2-maintenance
git cherry-pick abc1234
Handling Conflicts
If the cherry-pick creates a conflict:
git cherry-pick abc1234
# CONFLICT!
# Option 1: Resolve and continue
# Edit conflicted files
git add resolved-file.js
git cherry-pick --continue
# Option 2: Abort
git cherry-pick --abort
# Option 3: Skip this commit
git cherry-pick --skip
Cherry-Pick vs Merge vs Rebase
| Method | Use Case |
|---|---|
| Merge | Integrate entire branch |
| Rebase | Move/replay commits onto another branch |
| Cherry-pick | Copy specific commits |
When NOT to Cherry-Pick
- If you need the whole branch → Use merge
- If you're updating your branch → Use rebase
- If commits depend on each other → Cherry-pick all of them
Cherry-Picking a Range
# Commits: A ← B ← C ← D ← E
# Pick C, D, E (excludes B)
git cherry-pick B..E
# Pick B, C, D, E (includes B)
git cherry-pick B^..E
Cherry-Pick and Merge Commits
Merge commits have two parents. You must specify which parent's diff to use:
# -m 1 means "relative to first parent"
git cherry-pick -m 1 merge-commit-hash
Usually:
-m 1: Changes from the merged branch-m 2: Changes from the base branch
Tracking Cherry-Picks
Add Reference in Message
git cherry-pick -x abc1234
Adds:
Original commit message
(cherry picked from commit abc1234)
Using Git Notes
# Add note to commit
git notes add -m "Cherry-picked from feature branch"
Exercise: Cherry-Pick Practice
Practice cherry-picking a commit:
Common Mistakes
Cherry-Picking Dependencies
If commit B depends on commit A:
# Wrong: Only pick B (might not work)
git cherry-pick B
# Right: Pick both
git cherry-pick A B
Cherry-Picking Same Commit Twice
If you cherry-pick the same changes twice, you might get conflicts:
main: ... E' ... (cherry-picked E)
↓
Merge feature with original E
↓
Conflict: Same changes exist twice!
Use merge or rebase if you plan to merge the branch later.
Best Practices
- Use sparingly: Cherry-pick is powerful but can create confusion
- Document: Use
-xto track origin - Consider alternatives: Merge or rebase might be better
- Test thoroughly: Cherry-picked code might lack dependencies
- Don't double-merge: Be careful if branch will be merged later
Summary
- Cherry-pick copies specific commits to current branch
- Use
git cherry-pick <hash>for single commits - Use ranges for multiple commits
- Use
-xto document where commits came from - Handle conflicts like merge conflicts
- Good for hotfixes, backports, and selective changes
- Be careful about dependencies and future merges
In the next lesson, we'll learn about stashing changes.
التالي · الدرس 32 من 40
Stashing Changes
قراءة 4 دقيقة
أنهيت 31 من 40 درسًا

اسأل Byte عن هذا الدرس
محادثة خاصة حول هذا الدرس. قرأ Byte الدرس كاملًا، ويجيب خلال ثوانٍ، ويُظهر القسم الذي تأتي منه كل إجابة. يمكنك السؤال بأي لغة.
الأسئلة والأجوبة
أسئلة عامة حول هذا الدرس. يجيب AI Tutor فورًا بلغتك، ويراجع فريق FreeAcademy الإجابات، ويمكن لكل المتعلمين قراءتها.

