Skip to content

Lesson 12 of 14 · 15m

cherry-pick: Moving Single Commits on Purpose

Copy a patch to another branch with -x provenance — the backport pipeline the Linux stable tree runs on.

A CVE fix lands on main at 16:00 and must ship on release/1.8 tonight — but the rest of main can't come with it. git cherry-pick <sha> reads the diff between that commit and its parent and applies it as a new commit on your current HEAD: same patch, same author line, new SHA — author name, email, and date all carry over verbatim; what changes is the committer (you) and the committer date (now). git show --pretty=fuller prints AuthorDate next to CommitDate, which is how a backport still proves when the fix was originally written. It's a copy, not a move — the original stays put — which makes it the tool for "this change, over there, without the baggage."

Mechanics

git switch release/1.8
git cherry-pick -x a1b2c3d
  • -x appends (cherry picked from commit a1b2c3d...) to the message. For anything crossing a release boundary this is not optional — it's the only provenance record between "the fix on main" and "the fix on 1.8," and it's how audits and future bisects stay honest.
  • Ranges pick multiple commits: git cherry-pick A..B applies every commit after A through B — exclusive of A, an open interval that bites everyone once (A^..B or A..B with a merge-base is the fix).
  • -m 1 picks a merge commit — you name the mainline parent, same as revert.
  • -n/--no-commit applies the changes to your index without committing — accumulate several picks into one backport commit.
  • -e edits the message; --signoff adds your Signed-off-by trailer, which some projects require on every backport.

A conflicted pick leaves you in a sequencer state — git status says "cherry-pick in progress," resolve, git add, git cherry-pick --continue. --abort returns to the start; --skip drops the current pick. If git says "the previous cherry-pick is now empty," the change was already there — --skip it.

The duplicate-commit problem

Same patch, two SHAs. When the branches later merge, git usually reconciles identical changes cleanly — but rebase can re-apply them into fights, and git log gets confusing. Two commands keep the bookkeeping straight: git cherry upstream branch lists the branch's commits by whether their patch exists upstream (+ missing, - present — it compares patch-ids, not SHAs), and git log --cherry-pick A...B suppresses duplicates in a three-dot view. Useful before backporting and before re-merging a partially-picked branch.

The real-world version

This is the Linux stable tree's whole job. Fixes land on mainline, get tagged Fixes: or nominated Cc: stable@vger.kernel.org, and are cherry-picked onto each maintained release branch by the thousands — every stable kernel's changelog is a museum of (cherry picked from commit ...) lines. Documentation/process/stable-kernel-rules.rst is the written policy. Your release/1.8 backport is the same pipeline at smaller scale: fix forward on main, pick backward with -x.

The contrast worth internalizing: cherry-pick copies a commit forward; revert copies an inverse; rebase replays a whole branch; merge takes everything. "Ship the fix without the feature" is always the pick.

Pitfalls

  • Picking a commit whose context moved. If the target branch renamed the function or moved the file, the pick conflicts where the original applied clean — resolve against the base like any merge.
  • Empty picks happen on already-applied patches — --skip, don't --allow-empty unless the commit is deliberately empty.
  • -x on private branches is noise. Record provenance where it crosses a boundary someone will audit; skip it for local shuffling.
  • Picking merge commits without -m fails loudly ("is a merge but no -m option was given") — deliberately; which parent defines "the change" is a question only you can answer.

Try this

On a scratch repo with main and release branches, commit a fix on main, git cherry-pick -x it onto release, and read the message. Then git cherry the branches against each other and watch patch-id deduplication do its job.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.