Backporting a fix to a release branch, or rescuing one commit from an abandoned branch, is a cherry-pick.
`git cherry-pick <commit>` takes the diff a commit introduced and applies it as a new commit on your current branch, with the same message and author. The original stays where it was; the copy gets a new ID.
The classic use is backporting: a bug was fixed on main, and you need the same fix on release/2.3. You switch to the release branch and cherry-pick the fix commit.
Variants:
git cherry-pick A B C: several commits, applied in order.git cherry-pick A..B: a range, excludingAitself (useA^..Bto include it).-xappends "(cherry picked from commit ...)" to the message. Use it for backports so the link is traceable.-n/--no-commitapplies changes without committing, so you can combine several picks into one commit.-m 1picks a merge commit relative to its first parent.
Conflicts are handled like in rebase: fix, git add, git cherry-pick --continue; or --skip / --abort.
Use it sparingly. Cherry-picked copies are separate commits, so if both branches are later merged, Git sees the same change twice. Usually it merges cleanly, but it can produce confusing conflicts, and git log shows the change twice. Prefer merging the fix branch into both places when your workflow allows it; cherry-pick is for the cases where merging would bring in too much.
git log --cherry-pick --oneline A...B hides commits whose changes appear on both sides, useful for spotting what was already backported.
Example
git switch release/2.3
git cherry-pick -x 7e1f3c2
# [release/2.3 4a9b0d1] Fix null check in parser
# (cherry picked from commit 7e1f3c2...)
git cherry-pick main~5..main~2 # 3 commits: main~4, main~3, main~2
# CONFLICT in parser.py
git add parser.py
git cherry-pick --continue
# What on main is not yet on release (ignoring equivalent picks)?
git log --oneline --cherry-pick --right-only release/2.3...mainRemember
- cherry-pick copies a commit's change as a new commit on the current branch
- A..B excludes A; use A^..B to include it
- Use -x for backports so the origin is recorded
Watch out for
- Using cherry-pick as the normal way to move work between long-lived branches — duplicate commits make later merges confusing.