Git Cherry-Pick: Apply One Selected Commit
Bring a fix, not an entire branch
Imagine main contains a new gallery and a correction to the watering instructions. The older release needs only the correction. Cherry-pick applies the change introduced by a selected commit to your current branch, ordinarily recording a new commit. It does not import the entire source ancestry. A backport is a change adapted to an older supported release.
Inspect the source and target
Use the disposable setup in lab-fixtures/chapter18-guide.md, scenario A. The release branch has its own release.txt commit; main adds gallery.txt, then fixes notes.txt. The tag fix-source identifies only the fix. The graph has a shared base A, main commits B (gallery) and F (fix), and release commit R. Start with a clean working tree and confirm the destination. The following commands inspect the selected patch and preserve a rescue reference to the release tip.
git switch releasegit status --shortgit show fix-sourcegit branch rescue-releaseReplay the change
On release, cherry-pick fix-source. The new commit F′ has R as its parent, not B. The source tag and main stay unchanged. The notes become Water every 3 days, release.txt survives, and gallery.txt remains absent. Git usually preserves source author metadata, but records a new committer and parent; the commit ID differs in this divergent example. The --ff option is an exception that can reuse a commit when its parent equals HEAD.
git cherry-pick -x fix-sourcegit show HEAD:notes.txtgit log --graph --oneline --allgit diff --name-status rescue-release HEADCheck the dependency
A selected commit may depend on earlier changes, even if the application has no conflict. Review the source patch, dependencies and target tests. Do not assume a clean cherry-pick proves correctness. Practice: predict whether the gallery appears in release. It does not; only notes.txt changes in this fixture. To take all integrated work instead, consider merging the source branch.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Cherry-Pick: Apply One Selected Commit — knowledge check
Reason about selective integration and backport verification.
What does ordinary cherry-pick transfer?
Which branch receives the new commit?
What is the new parent in the divergent example?
Does a clean pick prove dependency compatibility?
What remains absent after selecting the fix alone?