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.
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.
git switch maintenance
git cherry-pick <source-commit>
git log --oneline --graph --decorate --allQuick 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.
git rev-parse <source-commit>
git cherry-pick <source-commit>
git rev-parse HEADQuick 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.
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.
git cherry-pick <commit-A> <commit-B>
git log --oneline --decorate -2Quick 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.
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.
git cherry-pick <commit>
git status
git rev-parse --verify CHERRY_PICK_HEADQuick 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.
git cherry-pick --continue
git cherry-pick --skip
git cherry-pick --abort
git cherry-pick --quitQuick 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.
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.
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.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.