History
Cherry-pick
8 min read
Cherry-pick copies a commit's changes to another branch, as a new commit. It's the way to take a fix
made on main to release/2.4 without bringing along the rest of what's on main, or to reuse a
commit from a branch that won't be merged.
The original commit doesn't change: cherry-pick only adds commits to the destination. That's why it doesn't require a force push.
In Versora there are three ways to cherry-pick:
| Way | Destination | Needs the destination checked out? |
|---|---|---|
| Cherry-pick this commit (commit menu) | The current branch | It's already the current one |
| Cherry-pick N commits (selection menu) | The current branch | It's already the current one |
| Dragging the commit onto a branch label | Any local branch | No, with git 2.40 or newer |
Applies a commit from another branch to the current branch.
- A commit's menu (right-click in the graph) › Cherry-pick this commit. The item is disabled when the commit is already in the current branch's history.
- Check out the branch that will receive the commit, for example
release/2.4(see Checkout). - In the graph, right-click the commit you want to copy, for example
a1b2c3d Fix tax ID validation, onmain. - Choose Cherry-pick this commit.
- Versora predicts whether there will be a conflict. If not, the cherry-pick runs right away ("Cherry-picking…"). If so, it first shows the conflicted files and asks (see "Cautions and messages").
- The new commit shows up at the top of
release/2.4.
git cherry-pick a1b2c3d
It's the usual cherry-pick, without -x: the new commit's message is the same as the original's
(without the "cherry picked from commit…" line), the author stays the same and you show up as the
one who made the commit (committer). If the repository signs commits (commit.gpgSign), the new
commit is signed too.
If there's a conflict, git stops midway. The changes panel shows the "Cherry-pick in progress" bar, with Continue, Skip and Abort (see Operation in progress).
- It finished without a conflict: Ctrl+Z ("Undo: Cherry-pick a1b2c3d") removes the new commit from the branch.
- It stopped at a conflict: Abort puts everything back as it was. If you resolved it and finished the cherry-pick (with Continue, through the commit box or even from the terminal), it goes into Ctrl+Z as a whole when it finishes, with the same name ("Undo: Cherry-pick a1b2c3d"), and undo removes the new commit. The steps taken during the stop (resolving a file in the conflict editor, for example) leave the undo history at that point. If Ctrl+Z refuses (the repository changed afterwards), use reset to the previous commit or the reflog.
Conflict preview (when it's on and with git 2.40 or newer):
- The title "The cherry-pick will conflict", with "Cherry-picking a1b2c3d ("Fix tax ID validation") will conflict in 2 files:" and the list of files (up to eight; the rest becomes "• and N more").
- "If you go ahead, the operation stops at the conflict and you resolve it in the conflict editor (Abort puts everything back)."
- Cherry-pick anyway goes ahead; Cancel does nothing.
The preview is only a help: if it fails or can't be done, the cherry-pick goes ahead as usual. It's explained in Predict conflicts before operating.
- Preferences › Operations › "Cherry-pick and revert" › Predict conflicts before cherry-pick and revert (on by default): "Simulates the operation in memory first (git merge-tree) and warns you if it will conflict, listing the files. Needs git 2.40 or newer."
- The company can hide or lock the Operations page through the policy (
ergoOpsinpreferences.hiddenorpreferences.locked); see Company policy.
- Revert: the opposite, a new commit that undoes the changes.
- Worktrees: "Cherry-pick into another worktree", in the commit menu.
Applies several commits to the current branch at once, from oldest to newest, each one as a new commit.
- Select the commits in the graph with Ctrl+click or Shift+click and open the selection menu › Cherry-pick 3 commits (the number follows the selection). See Multiple selection.
- With
release/2.4checked out, select the three commits of the fix onmain. - Right-click one of them and choose Cherry-pick 3 commits.
- If the preview finds a conflict, it says in which one: "Cherry-picking commit 2 of 3, b2c3d4e ("Adjust error message") will conflict in 1 file:". Confirm with Cherry-pick anyway or cancel.
- At the end: "Cherry-picked 3 commits."
git cherry-pick a1b2c3d b2c3d4e c3d4e5f # from oldest to newest
If one of them conflicts, git stops at it. Resolve it and click Continue to go on with the next ones, or Skip to leave that one out.
Ctrl+Z ("Undo: Cherry-pick 3 commits") removes the new commits. If the cherry-pick stopped, Abort puts everything back; if you took it to the end (with Continue and Skip), the whole cherry-pick goes into Ctrl+Z when the last commit goes in, and undo removes all the new commits at once.
When the selection doesn't work, the item shows the reason ("Cherry-pick 3 commits — reason"):
- "an operation is in progress": finish or abort whatever is stopped.
- "the selection includes a stash": take the stash out of the selection.
- "the selection includes a merge": cherry-pick from here only works for regular commits.
- "some commits are already on release/2.4": one of the commits is already on the current branch.
Takes one or more commits to another local branch without switching branches. The current branch and the open files don't change; only the destination branch moves forward. It's the fastest way to take a fix to two or three release branches in a row.
- Drag a commit from the graph (or several, if they're selected) onto the label of a local branch, in the graph or in the left panel. The label next to the cursor says "a1b2c3d Fix tax ID validation → cherry-pick onto release/2.4". The drag gesture is explained in Dragging branches and commits.
- Dropping onto a remote branch doesn't work: "… → can't commit to a remote branch".
- With
mainchecked out, select the commits of the fix (or just one). - Drag them onto the
release/2.4label. - The "Cherry-pick 2 commits onto release/2.4" window shows the plan: what goes in, on top of which commit, what is left out and the conflict prediction.
- Click Cherry-pick. At the end: "2 commits landed on release/2.4."
- "2 commits will land on release/2.4, on top of f0e1d2c, oldest first:" and the list.
- "1 commit is already in release/2.4 and is left out.": the commit is already an ancestor of the destination.
- "1 commit wouldn't change anything in release/2.4 (the changes are already there) and is left out.": the content is already in the destination (for example, from an earlier cherry-pick).
- "No checkout: the current branch and your files stay as they are; only release/2.4 moves."
- "release/2.4 is the current branch: this is a regular cherry-pick; on a conflict it stops in the conflict editor.": when you dropped onto the current branch itself.
- "No conflicts expected." or "Applying a1b2c3d (Fix tax ID validation) will conflict in 1 file:" with the list.
- "You can undo it with Ctrl+Z."
| Situation | Button | What it does |
|---|---|---|
| No conflict expected | Cherry-pick | Does the cherry-pick without checkout (or the regular one, if the destination is the current branch). |
| The destination is the current branch and a conflict is expected | Cherry-pick anyway | Does the regular cherry-pick, which stops at the conflict. |
| Another branch and a conflict is expected | Check out release/2.4 and cherry-pick | "With a conflict, the cherry-pick needs a checkout: the app checks out release/2.4 and stops at the conflict for you to resolve." |
| Git older than 2.40 | Check out release/2.4 and cherry-pick | "This git can't cherry-pick without a checkout: the app checks out release/2.4 first." |
On the current branch, it's the usual git cherry-pick. On another branch, each commit is applied
in memory, without touching the working directory:
# for each commit, from oldest to newest:
git merge-tree --write-tree --merge-base <commit's parent> <current tip of release/2.4> <commit>
git commit-tree <resulting tree> -p <current tip> # same author and date; -S if commit.gpgSign
# at the end, only if nothing changed along the way:
git update-ref -m "cherry-pick: 2 commit(s) (Versora)" refs/heads/release/2.4 <new tip> <old tip>
update-ref checks the old value: if release/2.4 moved in the meantime, nothing changes. With a
conflict, nothing changes and the notice explains why. A commit that wouldn't change anything in the
destination is skipped. The maximum is 100 commits per drag.
Ctrl+Z ("Undo: Cherry-pick 2 commits onto release/2.4") takes the destination
branch back to where it was. If you used Check out release/2.4 and cherry-pick and it stopped at a
conflict, Abort undoes the cherry-pick; the checkout stays done. When the operation finishes
(taken to the end with Continue, or aborted), it goes into Ctrl+Z with the
same name, counted from before the checkout: undo takes release/2.4 back to where it was and goes
back to the branch that was checked out before.
- "A merge can't be cherry-picked by dragging. Drag regular commits."
- "release/2.4 already contains these commits."
- "Too many commits at once. Use the interactive rebase.": more than 100 commits.
- "Nothing to do: release/2.4 already has these changes."
- "Cherry-picking a1b2c3d onto release/2.4 conflicts in 2 files. Nothing was changed.": the conflict showed up while it ran; nothing changed. Drag again and use the button with checkout.
- "Branch release/2.4 changed since the preview. Drag again to see the updated plan."
- "Cherry-picking onto a branch without checking it out needs git 2.40 or newer. Check out the branch and try again."
- "The tab changed: the plan was for another repository and was discarded. Drag again.": you switched tabs with the window open.
- Dragging doesn't start with a stopped operation in the repository (rebase, merge…).
- Preferences › Graph › Drag commits (on by default): "Onto a branch label: cherry-pick. Onto another commit: rebase with a preview. Always with a confirmation and a conflict prediction."
- The company can hide or lock the Graph page through the policy (
graph-viewinpreferences.hiddenorpreferences.locked); see Company policy.
- Dragging branches and commits: the gesture, rebase by dragging and the other destinations.
- Predict conflicts before operating
- Worktrees: "Cherry-pick into another worktree".