CurriculumAdvanced Gitgit cherry-pick
Lesson 2 of 633%
Advanced8 min+80 XP

git cherry-pick

Cherry-pick applies a specific commit from any branch onto your current branch. You are selecting exactly the change you want without merging the entire source branch. Perfect for applying hotfixes or extracting useful changes from unfinished work.

01.When Cherry-Pick Saves the Day

Common real-world scenarios: A critical security fix was committed to a feature branch — you need it on main and on the release branch right now. You accidentally committed to the wrong branch and need to move that one commit. A teammate wrote a useful utility function on their branch and you want it without taking all their WIP code.

Branch Visualization
featuremainHEAD
main
feature
HEAD

02.Cherry-Pick Creates New Commits

Cherry-pick always creates a brand new commit with a new SHA hash — the content is identical to the original but the metadata differs (hash, timestamp, parent pointer). The original commit is NOT moved or deleted. If conflicts occur during cherry-pick, resolve them the same way as merge conflicts, then run git cherry-pick --continue.

Commit Timeline
a1b2c3d
init
b2c3d4e
feat
c3d4e5f
fix
d4e5f6a
HEAD

Commands

commands — click to reveal output
$
git log --oneline feature/login
Find the exact commit hash you want
▼ show output
$
git checkout main
Switch to the branch you want to apply the commit to
$
git cherry-pick d4e5f6a
Apply that specific commit
▼ show output
$
git cherry-pick d4e5f6a..f9a3c21
Apply an inclusive range of consecutive commits
$
git cherry-pick d4e5f6a c3d4e5f b2c3d4e
Apply three specific non-consecutive commits
$
git cherry-pick --no-commit d4e5f6a
Apply changes staged but do not commit (review first)
$
git cherry-pick --abort
Abort mid-pick if something goes wrong
KNOWLEDGE CHECK

What happens to the original commit after cherry-picking it?

Rate this lesson:
PreviousNext Lesson