Basic usage
git switch release
git cherry-pick <sha>
# keep provenance in the message
git cherry-pick -x <sha>
# a range (exclusive start, inclusive end)
git cherry-pick A..B
Resolving conflicts
A cherry-pick applies a patch, so conflicts are normal when the target branch differs. Resolve them, stage the files, and continue. --abort returns to the state before the cherry-pick.
git status
git add <resolved-files>
git cherry-pick --continue
# skip this commit
git cherry-pick --skip
# give up
git cherry-pick --abort
When cherry-pick is the right tool
- A single fix must land on a release branch that cannot take a full merge.
- You need one commit from a long-lived feature branch without the rest.
- Backporting a security patch to multiple versions.
- Recovering a commit that was lost from a rebase (the original may still be in reflog).
The duplicate-commit trap
Cherry-picking creates a new commit with a different hash. Later merging the source branch will not recognize the change as already applied, which can produce conflicts or duplicate changes. If the branches will merge eventually, prefer merging or rebasing the whole branch; use cherry-pick for genuine backports.
-x appends “(cherry picked from commit …)” to the message. Use it for backports so future maintainers can trace the origin.
Cherry-pick a merge commit
Merge commits need -m 1 (or -m 2 for the other parent) to specify the mainline. Without it, Git refuses to pick the merge.
git cherry-pick -m 1 <merge-sha>