SovranCode
HomeCourses Git & GitHub Git Cherry-Pick
This device
Course contentsGit Cherry-Pick · 41 topics

1. Git Fundamentals

Git IntroductionGit Version ControlGit vs GitHubGit InstallationGit Working TreeGit RepositoryGit CommitGit .gitignoreGit Project: Learning Journal

2. Git History

Git LogGit DiffGit ResetGit RevertGit StashGit Project: Messy Repository

3. Branches & History

Git BranchGit MergeGit Merge ConflictsGit RebaseGit Cherry-PickGit TagsGit Project: Feature Branch

4. Remotes and GitHub

Git RemoteGitHub AuthenticationGitHub RepositoryGitHub ForkGitHub IssuesGitHub PagesGit Project: Two Clone Sync

5. GitHub Collaboration

GitHub Pull RequestsGitHub Code ReviewGit Branching WorkflowGitHub Branch ProtectionGitHub CollaborationGitHub ContributingGit Project: Reviewed Pull Request

6. Automation and Professional Git

GitHub ActionsGit InternalsGit RecoveryGitHub SecurityGit Project: Team Repository CIProject
Learn Git & GitHub40 complete · 1 planned

1. Git Fundamentals

Git IntroductionGit Version ControlGit vs GitHubGit InstallationGit Working TreeGit RepositoryGit CommitGit .gitignoreGit Project: Learning Journal

2. Git History

Git LogGit DiffGit ResetGit RevertGit StashGit Project: Messy Repository

3. Branches & History

Git BranchGit MergeGit Merge ConflictsGit RebaseGit Cherry-PickGit TagsGit Project: Feature Branch

4. Remotes and GitHub

Git RemoteGitHub AuthenticationGitHub RepositoryGitHub ForkGitHub IssuesGitHub PagesGit Project: Two Clone Sync

5. GitHub Collaboration

GitHub Pull RequestsGitHub Code ReviewGit Branching WorkflowGitHub Branch ProtectionGitHub CollaborationGitHub ContributingGit Project: Reviewed Pull Request

6. Automation and Professional Git

GitHub ActionsGit InternalsGit RecoveryGitHub SecurityGit Project: Team Repository CIProject
PREVIOUS LESSONGit Rebase
NEXT LESSONGit Tags
3. Branches & History 45 min

Git Cherry-Pick

Apply selected commits in a new context, resolve cherry-pick conflicts safely, and keep source identity in the new message.

What you will leave with

You will be able to select and apply a commit safely, backport with traceable provenance, handle conflicts and multi-commit picks, and prove the resulting commit. Naming that commit with a tag is the next lesson.

Select a change without merging a branch

SelectInspect one source commit and apply only its intended patch.
TracePreserve source identity in backports and account for replacement commits.
VerifyProve the destination patch, tests, and working state after the pick.

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.

SOURCE

A—B—C topic

C fixes a parser defect in the topic branch.

DESTINATION

R—S—C′ release

C′ applies that fix after S. It has a new parent and a new object ID.

Cherry-pick does not merge the source branch

It does not preserve the source branch’s topology or automatically include prerequisites from earlier commits. Selective application is safe only when the chosen patch makes sense in the destination context.

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>
  1. 01
    Confirm destination

    HEAD must name the branch that should receive the new commit.

  2. 02
    Require clean state

    Preserve unrelated index and working-tree changes before applying a patch.

  3. 03
    Read source intent

    Inspect parent, full patch, message, tests, and dependent commits—not only the subject line.

  4. 04
    Test 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 status

With 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.

QUICK CHECK

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 --abort

git 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^..B

Range 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.

Do not recreate a branch one pick at a time

Long manual sequences are easy to omit, duplicate, or reorder incorrectly. If the destination needs the whole development line, use the repository’s integration strategy.

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>
-m 1 is not a universal recipe

The parent number defines the baseline whose difference is replayed. Choosing it without reading parents can apply the opposite or an incomplete integration effect. Prefer the original focused commit when possible.

Prove the transferred change

  1. PATCH
    Intended difference

    Compare source and destination patches; explain context adjustments.

  2. TRACE
    Source recorded

    The backport message identifies the original commit and reason for transfer.

  3. BEHAVIOR
    Destination tests pass

    Exercise both the defect and compatibility promises of the target branch.

  4. STATE
    Focused clean result

    No 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 status

When 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

  1. 01
    Create two release lines

    In a disposable repository, let main advance while a release branch remains on an older compatible API.

  2. 02
    Author a focused fix

    Commit a tested parser correction on main and identify every dependency.

  3. 03
    Backport with provenance

    Pick with -x, adapt one intentional conflict, and test release compatibility.

  4. 04
    Prove the destination commit

    Compare source and destination patches, confirm tests, and keep the working tree focused.

Independent lab: audited patch transfer

  1. Create a disposable repository with main and release/1.3 histories, plus one transferable fix, one required prerequisite, and one unrelated feature.
  2. Prove why the fix and prerequisite belong in the backport while the feature does not.
  3. Use --no-commit to combine the two required patches into one release commit whose message records both source IDs.
  4. Produce an audit note containing source commits, destination patch, and test evidence.
Definition of done

The destination contains only the justified fixes, provenance is complete, and tests pass in the destination context. Tagging that commit is the next lesson.

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 -x or 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.

Related lessons

  • Git Rebase — Rebase replays a series; cherry-pick copies one change.
  • Git Tags — Name a verified commit after the change is in place.
KNOWLEDGE CHECK

Check your cherry-pick model

Choose how to transfer a patch and preserve provenance without merging the source branch.

01What does cherry-picking an ordinary commit create on the current branch?
02Why use git cherry-pick -x for a backport?
03What does git cherry-pick --no-commit do?
PREVIOUS LESSONGit Rebase
NEXT LESSONGit Tags
ON THIS PAGEGit Cherry-PickSelect a change without merging a branchWhat cherry-pick copies—and what it does notUse cherry-pick for a narrow transferInspect source, destination, and patch firstApply one traceable commitResolve a cherry-pick conflict in its new contextPick multiple commits with explicit orderCombine selected patches before one commitTreat cherry-picking a merge commit as exceptionalProve the transferred changeGuided practice: backport a focused fixIndependent lab: audited patch transferCommon cherry-pick mistakesLesson reviewKnowledge checkRelated lessons
Course contents