Git Cherry-Pick: Applying Specific Commits Across Branches

What Cherry-Pick Does

git cherry-pick <commit> takes the changes introduced by a specific commit and applies them to the current branch as a new commit. The new commit has a different hash — it is a copy, not a move.

Feature branch:  A → B → C → D → E
                                 ↑
                           cherry-pick D

Main branch:     A → B → X → Y → D'

D' has the same changes as D but a different parent and hash.

---

Basic Usage

# Apply a single commit
git cherry-pick abc1234

# Apply multiple specific commits
git cherry-pick abc1234 def5678

# Apply a range of commits (exclusive start, inclusive end)
git cherry-pick abc1234..def5678

# Apply but don't commit yet (--no-commit/-n)
git cherry-pick -n abc1234
# make additional changes if needed
git commit

---

The Main Use Case: Hotfixes

A bug exists in main (production) and in a v2-release branch (about to ship). You fix it on main, then cherry-pick to v2-release.

# Fix on main
git checkout main
git add -p         # stage the fix
git commit -m "fix: correct session timeout calculation"
git log --oneline -1
# abc1234 fix: correct session timeout calculation

# Backport to v2-release
git checkout v2-release
git cherry-pick abc1234
git push origin v2-release

---

Resolving Cherry-Pick Conflicts

git cherry-pick abc1234
# CONFLICT (content): Merge conflict in src/auth.js
# Resolve conflicts in editor, then:
git add src/auth.js
git cherry-pick --continue    # complete the cherry-pick

# Or abort and go back to where you were
git cherry-pick --abort

---

When to Cherry-Pick vs Merge vs Rebase

Use cherry-pick when:

Avoid cherry-pick when:

---

Picking from a Merged PR

# Find the commit hash from the PR
git log --oneline origin/main | grep "feat: add OAuth"
# 9f3e1a2 feat: add OAuth support (#142)

git cherry-pick 9f3e1a2

If the PR was merged with "Squash and merge", there is only one commit to pick. If it was a regular merge commit, use -m 1 to specify the mainline parent:

git cherry-pick -m 1 <merge-commit-hash>

Practice cherry-pick in the ShellGenius Git Labs — the git-undo-changes challenge covers recovering commits and undoing mistakes in real scenarios.