Cherry-Pick Commits
What you'll build
A scenario where a bug fix committed to a feature branch is cherry-picked to main and a release branch without merging the whole feature.
Goals
- Cherry-pick a single commit by hash
- Cherry-pick a range of commits
- Resolve conflicts during cherry-pick
- Understand when to cherry-pick vs merge
Prerequisites
- Basic Git commits and branches
- Commit hashes (git log)
- Conflict resolution basics
Steps
- 1
Set up branches with a shared bug
Create a feature branch with several commits, one of which is a bug fix. You want that fix on main now, without the rest of the feature.
Codegit checkout -b feature/big-feature echo 'feature work' >> feature.js && git add . && git commit -m 'Add feature' echo 'bug fix' >> bug.js && git add . && git commit -m 'Fix critical bug' git log --oneline # note the hash of 'Fix critical bug'Expected result
A feature branch with at least 2 commits; you've noted the bug fix hash.
- 2
Cherry-pick the fix to main
Switch to main and cherry-pick the bug fix commit by its hash. Git applies just that commit's changes, creating a new commit on main.
Hint
The cherry-picked commit gets a new hash on main, but the same changes and message.
- 3
Cherry-pick to a release branch
Create a release branch (e.g. release/v1.2) from an older commit. Cherry-pick the same fix there so the released version also gets the bug fix.
Hint
Use git checkout -b release/v1.2 <old-commit> then git cherry-pick <fix-hash>.
- 4
Handle a conflict
If the cherry-pick conflicts (the surrounding code differs on the target branch), Git pauses. Resolve the conflict, git add the file, and git cherry-pick --continue.
Hint
If you want to abort the cherry-pick instead, run git cherry-pick --abort to return to the previous state.
- 5
Cherry-pick a range
Cherry-pick multiple commits at once with git cherry-pick A..B (exclusive of A, inclusive of B). Verify all commits land on the target branch.
Codegit cherry-pick <hash-A>..<hash-B> git log --oneline # all commits in the range are now on this branchExpected result
All commits in the range appear on the target branch.
Try it yourself
# Run the commands above in a test repositorygit cherry-pick <commit-hash>What you learned
You learned cherry-pick — the surgical tool for applying specific commits across branches, essential for maintaining release branches and backporting fixes.
Related Resources
Related Lessons
- Branching
Cherry-pick moves commits between branches.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.