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-xappends(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..Bapplies every commit afterAthroughB— exclusive ofA, an open interval that bites everyone once (A^..BorA..Bwith a merge-base is the fix). -m 1picks a merge commit — you name the mainline parent, same as revert.-n/--no-commitapplies the changes to your index without committing — accumulate several picks into one backport commit.-eedits the message;--signoffadds yourSigned-off-bytrailer, 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-emptyunless the commit is deliberately empty. -xon private branches is noise. Record provenance where it crosses a boundary someone will audit; skip it for local shuffling.- Picking merge commits without
-mfails 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?
Keep reading
Related articles from the SudoSecurity blog.
What is Git? The Version Control System Every Developer Uses
Git tracks every change to every file in your codebase, lets multiple developers work simultaneously without overwriting each other, and gives you a complete history you can rewind to any point. It is the foundation of modern software development. Here is how it actually works.
VSCode 1.72: Git Source Control has some good enhancements.
The latest version of Microsoft’s Visual Studio Code code editor focuses on improvements to the toolbar and autoscrolling, as well as Git repos. Unveiled October 6, Visual Studio Code 1.72 is also known as the September 2022 release, as it features fixes and features from last month. Toolbar
Apple's WebKit comes to GitHub
The development of WebKit, the open source web browser engine at the heart of Apple’s Safari web browser, has migrated over to GitHub. The WebKit project team announced August 31 that the project had frozen its Subversion tree on June 23 and transitioned management and interaction with the source
GitHub Open Sources Entitlements IAM
GitHub is making available a new IAM (Identity and Access Management) tool, dubbed Entitlements, which leverages the company's own Git framework to parse, track and approve access to a business' systems. The basic idea of Entitlements is to use a dedicated Git repository as a way to provide a…