Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Git Cherry-Pick: Applying Selected Commits to Another History

Apply selected existing commits to the current branch, understand new commit identities, control in-progress cherry-picks, and verify selective history transformations.

Levels: Advanced ~40 min read

Advanced

Apply one existing commit

`git cherry-pick <commit>` applies the change introduced by the selected commit to the current branch and normally records a new commit. The source commit remains in its original history.

bash
git switch maintenance
git cherry-pick <source-commit>
git log --oneline --graph --decorate --all

Quick check

What is the normal result of cherry-picking a non-merge commit?

A source commit exists on another history line.The current branch receives a new commit containing its change.

The source commit is not moved

Cherry-pick applies a commit’s effect in a new history context. It does not move the source branch or make the destination branch point at the original source commit.

bash
git rev-parse <source-commit>
git cherry-pick <source-commit>
git rev-parse HEAD

Quick check

Why is the destination commit normally different from the source commit?

The source commit belongs to another history.The selected change is recorded in the destination history.

Use --no-commit to inspect or combine changes

`git cherry-pick --no-commit <commit>...` applies the selected changes to the working tree and index without automatically creating the commits. This lets you inspect the staged result or choose a different commit boundary.

bash
git cherry-pick --no-commit <commit>
git diff --cached
git status --short
git commit -m "Combined change"

Quick check

What does `--no-commit` change?

A selected commit introduces a change.The change is staged without a cherry-pick commit.

Cherry-pick more than one commit

Multiple commit arguments can be supplied. Git normally processes them as a sequence and records a new commit for each successfully applied change.

bash
git cherry-pick <commit-A> <commit-B>
git log --oneline --decorate -2

Quick check

What normally happens when two independent commits are cherry-picked?

Two commits introduce distinct changes.The destination history contains two newly created commits.

Establish a clean state first

The ordinary cherry-pick operation requires the working tree to be clean with respect to `HEAD`. This prevents unrelated local modifications from being mixed into the operation.

bash
git status --short
git cherry-pick <commit>

Quick check

Why establish a clean state before an ordinary cherry-pick?

Local modifications exist outside the selected commit.A clean state is established before cherry-picking.

Conflicts create an in-progress cherry-pick

If a selected change cannot be applied cleanly, Git stops. `CHERRY_PICK_HEAD` identifies the commit whose change failed to apply, while the index and working tree record the unresolved state. Detailed conflict resolution is owned by the existing conflict lessons.

bash
git cherry-pick <commit>
git status
git rev-parse --verify CHERRY_PICK_HEAD

Quick check

What identifies a cherry-pick waiting for resolution?

The selected change conflicts with the destination history.Git records an in-progress cherry-pick state.

Control an in-progress sequence

The sequencer provides `--continue`, `--skip`, `--abort`, and `--quit`. Continue resumes after resolution; skip omits the current commit; abort cancels the sequence and returns to the pre-sequence state; quit forgets the sequencer state without resetting the worktree and index to the pre-sequence state.

bash
git cherry-pick --continue
git cherry-pick --skip
git cherry-pick --abort
git cherry-pick --quit

Quick check

Which command cancels the sequence and returns to the pre-sequence state?

A cherry-pick sequence is in progress.The sequence is canceled and the pre-sequence state is restored.

Cherry-picking a merge requires a mainline

A merge commit has multiple parents. `git cherry-pick -m <parent-number> <merge-commit>` selects the parent that is treated as the mainline so Git can define the merge commit’s change relative to that parent.

bash
git show --no-patch --format=%P <merge-commit>
git cherry-pick -m 1 <merge-commit>

Quick check

Why does a merge commit require `-m <parent-number>`?

The selected object is a merge commit.The chosen parent defines the mainline.

Verify the destination history

After a successful cherry-pick, inspect the destination branch, new commit identity, parent, and resulting change. The source commit should remain where it was; the destination commit should normally have a different identity.

bash
git rev-parse HEAD
git show --stat --oneline HEAD
git show <source-commit>

Quick check

What should verification establish?

The command completed successfully.The destination history and resulting change are inspected.

Practice what you learned

Use the dedicated Git code-completion exercise for this lesson.

Was this page helpful?

Mark this learning resource complete to track your learning progress.