git cherry-pick: Backporting Fixes Between WordPress Plugin Branches

git cherry-pick applies the changes from one or more specific commits onto a different branch. It is the correct tool when you need to backport a bug fix to a release branch without merging an entire feature branch — a common scenario in WordPress plugin maintenance.

Problem: A bug fix committed on the main branch needs to be applied to an older release/2.x support branch without merging the entire commit history between the two branches.

Solution: Use git cherry-pick <commit-hash> to apply the exact changes from a specific commit onto the current branch. For multiple commits, specify a range with git cherry-pick A..B. If conflicts arise, resolve them normally and complete the pick with git cherry-pick --continue.

The examples below cherry-pick a single fix commit onto a release branch, handle a conflict during cherry-pick, and pick a range of commits while preserving their original commit messages.

# Scenario: fix was committed to 'develop', need it in 'release/2.x'
git log --oneline develop | head -5
# abc1234 fix: correct nonce verification in REST endpoint
# def5678 feat: add new dashboard widget
# ...

# Switch to the release branch
git checkout release/2.x

# Cherry-pick just the fix commit
git cherry-pick abc1234

# Cherry-pick multiple commits (applied oldest-first)
git cherry-pick abc1234 bcd2345 cde3456

# Cherry-pick a range (exclusive..inclusive)
git cherry-pick abc1234..def5678

# Keep original author and date, add a note about the backport
git cherry-pick -x abc1234
# -x appends "cherry picked from commit abc1234" to the commit message

Handle conflicts and abort/continue during cherry-pick:

# If cherry-pick hits a conflict:
# CONFLICT (content): Merge conflict in src/class-rest-endpoint.php
# Auto-merging src/class-rest-endpoint.php

# 1. Resolve the conflict in the file
# 2. Stage the resolved file
git add src/class-rest-endpoint.php

# 3. Continue the cherry-pick
git cherry-pick --continue
# Your $EDITOR opens to confirm/edit the commit message

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

# Cherry-pick without auto-committing (stage only, let you amend)
git cherry-pick --no-commit abc1234
git status       # review the staged changes
git commit -m "backport: fix nonce verification (from abc1234)"

# Cherry-pick a merge commit — must specify which parent to diff against
# -m 1 means "treat parent 1 (the branch you merged into) as the main line"
git cherry-pick -m 1 merge-commit-hash

NOTE: Prefer cherry-pick over manual patch application when the source and target branches share history — Git tracks the picked commits by hash, making it easy to see which fixes have been backported with git log --oneline release/2.x and grepping for "(cherry picked from".

Leave Comment

Your email address will not be published. Required fields are marked *