What is a cherry-pick?
Copying the change one commit made onto the branch you are on, as a new commit of its own.
A merge brings everything another branch has; a cherry-pick brings one commit. Git works out what that commit changed compared with its parent, makes the same change on the branch you are on, and records it as a new commit, with the original’s message and author. The original stays where it was, and so does the rest of its branch.
What arrives is a copy, not the commit itself. It is committed at a different
time, and usually on a different parent, so it has an id of its own: to Git,
the original and the copy are two commits that happen to make the same change. release/1.0 was cut from main at B and has R, a commit of its own. main has moved on: C is a new feature, D a fix the release needs too. You are on release/1.0. Cherry-pick D. Git works out what D changed from C, makes the same change on top of R, and commits it as D′, with D's message and an id of its own. release/1.0 moves to D′; C stays on main. Later, release/1.0 is merged into main. Both branches made the same change to the same lines, so the merge takes it once and goes through. main's history now holds the fix twice, as D and D′.
When it is the right tool
Git’s own guide to workflows puts the difference in a line: merging works at the branch level, cherry-picking at the commit level. A cherry-pick is for the commit you want without the ones around it:
- a fix made on main that a release branch needs too, which is a backport;
- a commit made on the wrong branch, copied to the right one before it is taken off the wrong one;
- one commit worth keeping from a branch that will never be merged.
For more than that, merge or rebase. The same guide tries to solve as much as possible with merges, and gives fixes a rule of their own: make the fix on the oldest branch that needs it, then merge that branch into the newer ones, so that the fix exists once. A cherry-pick carries a fix the other way, when it was made on the newer branch first. The guide expects that to happen a few times, and calls it nothing to worry about unless it happens very often.
What it leaves behind
Once both branches have the fix, there are two commits for it, and nothing in the copy points at the original. Ask Git which branches contain D and it answers main alone: release/1.0 has D′, which is a different commit. Two things make the link visible again:
-xends the copy’s message with a line,(cherry picked from commit …), that names the original. Git’s documentation suggests it between two published branches, as in a backport, and advises against it for a commit from a private branch, whose id means nothing to anyone else.git cherrycompares changes rather than ids. Given two branches, it lists the commits of the second that the first does not contain, with-before each one whose change the first has all the same, and+before the rest.
A later merge of the two branches finds the same change on both sides and takes it once, as in the last step above. That holds while the lines stay as the copy left them: change them again on either branch before the merge and the two sides differ there, so the merge stops on a conflict, as any merge does when the two sides disagree.
When it stops
A cherry-pick brings the change, not the commits it was built on, so it stops
on a conflict when the lines it changes read differently, or do not exist, on
the branch you are on. Several commits are copied one at a time, so it can
stop more than once, as a rebase can. Resolve the files as in a merge, and
--continue carries on.
It also stops on a commit whose change the branch already has, because the
copy would be empty: --skip leaves that commit out, and
git commit --allow-empty records it anyway. With --empty=drop, such
commits are left out without stopping. At any of these points, --abort puts
the branch back where it was before the first copy.
A merge is refused until you say which parent to measure it from: it has more
than one, and what it changed depends on the choice. -m 1 measures from the
first parent, the branch the merge was made on, and copies everything the
merge brought into it.
On the command line
git switch release/1.0 # with a clean working tree
git cherry-pick <commit> # copy one commit onto release/1.0
git cherry-pick -x <commit> # …naming the original in the message
git cherry-pick <a> <b> # several, in the order given
git cherry-pick main~3..main # main's last three, oldest first
git cherry-pick -m 1 <merge> # a merge, from its first parent
git cherry-pick --continue # once the conflict is resolved
git cherry-pick --abort # back to where it started
git cherry -v release/1.0 main # - copied already, + still to copy
Git’s own reference: git-cherry-pick and git-cherry. The Pro Git book likens a cherry-pick to a rebase for a single commit, and gitworkflows explains merging upwards.
In Innesta
Cherry-picking commits is where Innesta runs one: what it predicts before anything changes, including the commits your branch already has, and when it names the original in the copy’s message. A cherry-pick that stops on a conflict is settled in the three-pane editor, and moving a change to another branch is the answer for a commit made on the wrong branch.
The Playground is a repository made for trying this on: its develop branch starts with a commit that main does not have.