Git Cherry-pick
The git cherry-pick command takes a specific commit from one branch and applies it to the current branch. Instead of merging an entire branch, a single commit's changes can be "picked" and applied anywhere.
Real-life analogy: Imagine a bowl of mixed fruit. Cherry-picking means reaching into the bowl and taking only the cherry — not the apple, not the banana, just that one specific fruit. In Git terms, taking one specific commit and applying it to a branch without bringing in all the other commits is exactly what cherry-pick does.
When is Cherry-Pick Useful?
- A bug fix on a development branch needs to be applied to a production branch immediately, without merging all the unfinished development work
- A useful commit accidentally made on the wrong branch needs to be moved to the correct branch
- Specific features from an experimental branch need to be brought to main without merging the whole branch
Basic Syntax
git cherry-pick <commit-hash>Basic Example
# History of branches:
# main: A ──── B ──── C
# develop: A ──── B ──── D ──── E ──── F
# ↑
# This is a critical bug fix
# Currently on main, want to apply only commit E
git switch main
git cherry-pick <hash-of-E>
# Result:
# main: A ──── B ──── C ──── E' (copy of E applied to main)
# develop: A ──── B ──── D ──── E ──── F (unchanged)
Step-by-Step Practical Example
# View the develop branch log to find the commit hash
git log develop --oneline
# f1a2b3c Add search feature
# e4d5c6b Fix critical null pointer bug ← Need this one
# d7e8f9a Start new feature
# Switch to main
git switch main
# Cherry-pick the bug fix commit
git cherry-pick e4d5c6b
# Output:
[main a3f92b1] Fix critical null pointer bug
Date: Mon Oct 9 14:32:10 2023
1 file changed, 3 insertions(+), 1 deletion(-)
The bug fix is now on both main and develop. The commit on main gets a new hash (it is a copy, not the original).
Cherry-Pick Multiple Commits
Pick Several Specific Commits
git cherry-pick a1b2c3d e4f5g6hEach commit is applied in the order listed.
Pick a Range of Commits
git cherry-pick a1b2c3d..e4f5g6hApplies all commits from after a1b2c3d up to and including e4f5g6h.
Cherry-Pick Without Committing
To apply the changes but not create a commit yet (for review or modification):
git cherry-pick e4d5c6b --no-commitThe changes are staged but not committed. A commit can then be made manually.
Cherry-Pick with a Custom Message
git cherry-pick e4d5c6b --editOpens the editor to customize the commit message before applying.
Handling Conflicts During Cherry-Pick
If the cherry-picked commit conflicts with changes already in the target branch:
CONFLICT (content): Merge conflict in app.js
error: could not apply e4d5c6b... Fix critical null pointer bug
hint: After resolving the conflicts, mark them with
hint: "git add <pathspec>", then run "git cherry-pick --continue"
Resolve the conflict in the file, then:
git add app.js
git cherry-pick --continue
To cancel the cherry-pick:
git cherry-pick --abortCommon Practical Scenario — Hotfix on Production
Branch structure:
- main (production)
- develop (development)
- feature-new-ui (not stable)
Scenario: A critical bug was fixed in develop and needs to go to main NOW
without bringing in unfinished feature work.
# Step 1: Find the fix commit on develop
git log develop --oneline
# c3d4e5f Fix SQL injection vulnerability ← Critical fix
# b2c3d4e Add new UI components
# a1b2c3d Update database schema
# Step 2: Cherry-pick the fix to main
git switch main
git cherry-pick c3d4e5f
# Step 3: Push main to production
git push origin main
Cherry-Pick vs Merge
| Feature | Cherry-Pick | Merge |
|---|---|---|
| Applies | One specific commit | All commits from a branch |
| Preserves original commit | Creates a copy with a new hash | Original commits are part of the merge |
| Use case | Surgical, targeted changes | Combining full feature branches |
Summary
git cherry-pick applies the changes from a specific commit onto the current branch without merging the entire branch. It creates a new commit with the same changes but a different hash. This is ideal for applying urgent bug fixes to production branches or for moving commits that were made on the wrong branch. Use with care in shared environments to avoid creating duplicate commits.
