Git Cherry-Pick
Apply selected commits in a new context, resolve cherry-pick conflicts safely, and keep source identity in the new message.
Select a change without merging a branch
What cherry-pick copies—and what it does not
A normal commit represents a snapshot plus metadata and a parent. Cherry-pick computes the selected commit’s change relative to its parent, applies that change to the current branch, and creates a new commit.
A—B—C topic
C fixes a parser defect in the topic branch.
R—S—C′ release
C′ applies that fix after S. It has a new parent and a new object ID.
Use cherry-pick for a narrow transfer
Backport a verified fix
Apply one reviewed bug fix from main to a supported release branch without merging unrelated new work.
Recover a misplaced commit
Copy a local commit onto its intended branch, verify it, then correct the accidental branch separately.
Avoid branch integration
If the branches should share a full line of development, merge or rebase according to policy instead of picking many commits manually.
Check dependencies
A patch may compile only with preceding schema, API, test-fixture, or configuration commits.
Inspect source, destination, and patch first
git status
git branch --show-current
git log --oneline --decorate --graph --all
git show --format=fuller --stat <source-commit>
git show <source-commit>
git branch --contains <source-commit>
git diff <source-commit>^ <source-commit>- 01Confirm destination
HEAD must name the branch that should receive the new commit.
- 02Require clean state
Preserve unrelated index and working-tree changes before applying a patch.
- 03Read source intent
Inspect parent, full patch, message, tests, and dependent commits—not only the subject line.
- 04Test the baseline
Know whether the destination already fails before attributing failures to the pick.
Apply one traceable commit
git status
git show --stat --oneline <source-commit>
git show --format=fuller <source-commit>
# Apply the patch as a new commit and record its origin:
git cherry-pick -x <source-commit>
git show --stat --oneline HEAD
git statusWith a clean application, -x appends a “cherry picked from commit …” line to the new message. This is especially useful for public backports because it connects two different commit IDs without pretending they are the same object.
Test what you learned
Type the command that cherry-picks commit a1b2c3d and records its source ID in the new message.
Resolve a cherry-pick conflict in its new context
A patch that applied on the source parent may conflict or behave differently on the destination. Inspect the stopped commit and resolve for the destination branch’s supported behavior.
git status
git show CHERRY_PICK_HEAD
git diff --name-only --diff-filter=U
# Edit, test, and stage the intended destination result
git add <resolved-paths>
git diff --cached
git diff --cached --check
git cherry-pick --continue
# Or restore the complete pre-operation state
git cherry-pick --abortgit cherry-pick --skip omits the current commit in a multi-commit sequence. Use it only after proving the change is redundant or intentionally excluded.
Pick multiple commits with explicit order
# Explicit objects are applied left to right:
git cherry-pick -x <commit-one> <commit-two> <commit-three>
# Commits after A through B, excluding A:
git cherry-pick -x A..B
# Include A through B:
git cherry-pick -x A^..BRange notation selects a revision set; the order and topology may not match a simplistic visual reading in complex graphs. Preview the exact set with git log --reverse --topo-order A..B and prefer explicit IDs when auditability matters.
Combine selected patches before one commit
git cherry-pick --no-commit <commit-one> <commit-two>
git status
git diff --cached
# Adjust only when required by the destination, then test
git commit -m "Backport parser boundary fixes"--no-commit applies changes to the index and working tree without creating each original commit. The final commit no longer has automatic one-to-one provenance, so document all source IDs and explain why combining them is correct.
Treat cherry-picking a merge commit as exceptional
A merge has multiple parents, so Git needs a mainline parent to determine which combined change should be replayed.
git show --no-patch --pretty=raw <merge-commit>
git diff <merge-commit>^1 <merge-commit>
git diff <merge-commit>^2 <merge-commit>
# Only after identifying the correct parent semantics:
git cherry-pick -m 1 <merge-commit>Prove the transferred change
PATCHIntended differenceCompare source and destination patches; explain context adjustments.
TRACESource recordedThe backport message identifies the original commit and reason for transfer.
BEHAVIORDestination tests passExercise both the defect and compatibility promises of the target branch.
STATEFocused clean resultNo unrelated paths, unresolved entries, or working changes remain.
git show --format=fuller --stat HEAD
git diff HEAD^ HEAD
git diff <source-commit>^ <source-commit>
git statusWhen the transferred commit is the release you want to name, continue in Git Tags. This lesson stops at a proven cherry-pick.
Guided practice: backport a focused fix
- 01Create two release lines
In a disposable repository, let main advance while a release branch remains on an older compatible API.
- 02Author a focused fix
Commit a tested parser correction on main and identify every dependency.
- 03Backport with provenance
Pick with
-x, adapt one intentional conflict, and test release compatibility. - 04Prove the destination commit
Compare source and destination patches, confirm tests, and keep the working tree focused.
Independent lab: audited patch transfer
- Create a disposable repository with main and
release/1.3histories, plus one transferable fix, one required prerequisite, and one unrelated feature. - Prove why the fix and prerequisite belong in the backport while the feature does not.
- Use
--no-committo combine the two required patches into one release commit whose message records both source IDs. - Produce an audit note containing source commits, destination patch, and test evidence.
Common cherry-pick mistakes
Picking by message alone
Subjects omit dependencies and side effects. Inspect the parent-relative patch and tests.
Duplicating an existing patch
Search destination history and behavior before applying what may already be present differently.
Skipping conflicts reflexively
Skip discards the current patch. Prove redundancy before omitting it.
Recreating a branch one pick at a time
If the destination needs the whole line of development, merge or rebase according to policy.
Lesson review
You can now transfer a focused change while preserving its origin, handle destination-specific conflicts, and prove the resulting commit. Git tags, taught next, name that verified object without turning cherry-pick into a release button.
- I inspect source patch, dependencies, destination branch, and baseline before cherry-picking.
- I use
-xor explicit source IDs when backport provenance matters. - I understand multi-commit ranges, no-commit mode, and merge mainline selection.
- I prove the transferred change with patch, tests, and a focused working tree.