Skip to content

Git Cherry-Pick — Applying Specific Commits

DodaTech 3 min read

In this tutorial, you'll learn about Git Cherry. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

What You'll Learn

Use git cherry-pick to apply specific commits from one branch to another — useful for hotfixes, selective feature backporting, and recovering lost changes.

Why It Matters

Sometimes you need just one commit from a branch, not the entire branch. Cherry-pick lets you pluck individual commits — like a hotfix from main to a release branch.

Real-World Use

Applying a critical bug fix from main to a release branch without deploying everything else, or bringing a specific feature commit into an older version.

Basic Cherry-Pick

# On the target branch
git checkout release/v1

# Cherry-pick a commit from main
git cherry-pick a1b2c3

# a1b2c3's changes are applied to release/v1
# A new commit is created (different hash)

How It Works

Before:
main:       A---B---C---D---E
release/v1: F---G---H

git checkout release/v1
git cherry-pick C

After:
main:       A---B---C---D---E
release/v1: F---G---H---C'
                    C' applies C's changes

Cherry-Pick Multiple Commits

# Cherry-pick a range
git cherry-pick a1b2c3..d4e5f6
# Includes a1b2c3 through d4e5f6 (exclusive of a1b2c3)

# Cherry-pick a range (inclusive)
git cherry-pick a1b2c3^..d4e5f6

# Cherry-pick specific commits in order
git cherry-pick a1b2c3 d4e5f6 g7h8i9

Real-World Hotfix Scenario

# Production bug found — fixed on main
git checkout main
git commit -m "Fix null pointer in login handler"  # hash: f1e2d3
git push

# But the release branch needs it too
git checkout release/v2.1
git cherry-pick f1e2d3

# Resolve any conflicts
# push the release branch
git push origin release/v2.1

Cherry-Pick with Options

# Don't automatically commit (useful for editing)
git cherry-pick -n a1b2c3
# Changes are staged but not committed

# Add a custom message
git cherry-pick -x a1b2c3
# Appends "(cherry picked from commit a1b2c3)" to the message

# Include the original committer date
git cherry-pick --signoff a1b2c3

Resolving Cherry-Pick Conflicts

# Conflict occurs during cherry-pick
# Fix the conflicting files

git add file.txt             # Mark as resolved
git cherry-pick --continue   # Continue

# Or skip this commit
git cherry-pick --skip

# Or abort entirely
git cherry-pick --abort

Cherry-Pick from Any Ref

# Cherry-pick from a commit by hash
git cherry-pick a1b2c3

# Cherry-pick from another branch
git cherry-pick feature/login~2  # Third commit from tip

# Cherry-pick from a tag
git cherry-pick v1.0.0

# Cherry-pick from a reflog entry
git cherry-pick HEAD@{5}

When to Use Cherry-Pick

✅ Hotfix to a release branch
✅ Backporting a specific feature to an older version
✅ Recovering a commit that was accidentally deleted
✅ Applying a configuration change across environments

When NOT to Use Cherry-Pick

❌ Instead of a proper merge (merges keep full history)
❌ To copy every commit from one branch to another
❌ When the commits are interdependent (hard to cherry-pick)

Alternatives

Need Use
Apply entire branch git merge
Move branch base git rebase
Single commit to another branch git cherry-pick
Undo a commit git revert

Practice

# Create two branches with different commits
git init cherry-pick-lab
cd cherry-pick-lab
echo "initial" > file.txt && git add . && git commit -m "init"

git checkout -b feature-a
echo "feature A" >> file.txt && git add . && git commit -m "Add feature A"

git checkout -b feature-b main
echo "feature B" >> file.txt && git add . && git commit -m "Add feature B"

# Cherry-pick feature A's commit into feature B
git cherry-pick feature-a

# Check the log — feature B now has both commits
git log --oneline

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro