Cherry-picking
Overview
Sometimes you want one specific commit from another branch, not everything on it. A bug fix made on main needs to go into an older release. A commit landed on the wrong branch. git cherry-pick copies individual commits onto the current branch.
Picking a commit
Switch to the branch that should receive the commit, then name the commit:
git switch release/1.0
git cherry-pick 3c4d5e6[release/1.0 8f9e0d1] Escape user names in comments
Date: Fri Dec 4 10:12:44 2026 +0530
1 file changed, 1 insertion(+), 1 deletion(-)Git takes the changes that commit made (its diff against its parent) and applies them here, creating a new commit with the same message and author. Since the parent is different, the new commit has a new ID.
Recording where it came from
git cherry-pick -x 3c4d5e6-x appends a line to the message:
Escape user names in comments
(cherry picked from commit 3c4d5e6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e)Use it whenever you copy between public branches (like main to a release branch), so anyone can trace the fix back to its original commit and discussion.
Picking several commits
git cherry-pick a1b2c3d e4f5a6b # several specific commits, in that order
git cherry-pick a1b2c3d..e4f5a6b # a range: everything after a1b2c3d up to e4f5a6b
git cherry-pick --no-commit a1b2c3d # apply the changes, but let me commit myselfConflicts
If the receiving branch has changed the same lines, cherry-pick stops with a conflict, just like merge and rebase:
# fix the files, then
git add comments.js
git cherry-pick --continue
# or give up:
git cherry-pick --abortBackports to old release branches often conflict, because the surrounding code has changed. Sometimes it’s simpler to write the fix again by hand for the old version.
Common uses
Backporting a fix
The classic: you maintain release/1.0 for existing customers while main moves on. A fix lands on main and needs to ship as 1.0.1 without the new features. Cherry-pick the fix onto the release branch, tag it, release it.
Rescuing a commit from the wrong branch
You committed on main but meant to start a feature branch:
git switch -c feature/search # new branch keeps the commit…
git switch main
git reset --hard HEAD~1 # …and main forgets it (only if not pushed!)Or, if the right branch already exists:
git switch feature/search
git cherry-pick main # copy the commit main points to
git switch main
git reset --hard HEAD~1Taking one good idea from an abandoned branch
An experiment didn’t work out, but one commit in it is useful. Pick it, delete the branch.
When not to cherry-pick
- Instead of merging. If you’ll eventually merge the branch anyway, cherry-picking some of its commits first creates duplicates: the same change appears twice with different IDs. Git usually notices the duplicate when you merge later, but history gets confusing and conflicts become more likely.
- Long series of commits. Picking twenty commits one by one is a sign you want a merge or a rebase.
Remember: cherry-pick copies changes, not commits. It’s a precise tool for moving a fix between lines of development that aren’t meant to be merged.
Backport a fix to a release branch
main has moved on with new features since the release/1.0 branch was cut. A security fix was committed on main (“Escape user names in comments”). Copy just that fix onto release/1.0, recording where it came from with -x, without bringing any of the new features. Then tag the result as v1.0.1. Check that the release branch has the fix but not new-feature.txt.
mkdir backport && cd backport && git init -b main
echo "comments" > comments.js && git add . && git commit -qm "Add comments"
git branch release/1.0 # the 1.0 release is cut here
echo "big feature" > new-feature.txt && git add . && git commit -qm "Add big feature"
echo "comments (escaped)" > comments.js && git commit -qam "Escape user names in comments"
echo "another feature" >> new-feature.txt && git commit -qam "Extend big feature"
# 1. Find the fix commit's ID
# 2. Copy it onto release/1.0, with -x
# 3. Tag the patch release and checkmkdir backport && cd backport && git init -b main
echo "comments" > comments.js && git add . && git commit -qm "Add comments"
git branch release/1.0 # the 1.0 release is cut here
echo "big feature" > new-feature.txt && git add . && git commit -qm "Add big feature"
echo "comments (escaped)" > comments.js && git commit -qam "Escape user names in comments"
echo "another feature" >> new-feature.txt && git commit -qam "Extend big feature"
# 1. Find the fix commit's ID
git log --oneline main
fix=$(git log --format=%h --grep="Escape user names" main)
# 2. Copy it onto release/1.0, with -x
git switch release/1.0
git cherry-pick -x "$fix"
git log -1 --format=%B # message ends: (cherry picked from commit …)
# 3. Tag the patch release and check
git tag -a v1.0.1 -m "Security fix for comments"
cat comments.js # comments (escaped)
ls new-feature.txt 2>/dev/null || echo "no new features on the release branch"Summary
git cherry-pick <commit>applies one commit’s changes to the current branch as a new commit.- The copy has the same changes and message but a new ID;
-xrecords the original ID in the message. - Use it to backport fixes to release branches or to rescue a commit made on the wrong branch.
- Conflicts are handled like rebase: fix,
git add,git cherry-pick --continue(or--abort). - Don’t use it as a substitute for merging — duplicated commits make history confusing.