Cherry-picking
git cherry-pick copies a single commit from one branch onto your current branch.
Syntax
git cherry-pick <commit-hash>git cherry-pick applies the changes from one specific commit onto your current branch. It is handy for pulling a bug fix out of one branch without merging everything else.
How it works
You give it a commit hash; Git creates a new commit on your branch with the same changes. You can cherry-pick several commits at once.
Example
# Find the commit you want
git log --oneline other-branch
# Apply just that commit here
git switch main
git cherry-pick a1b2c3d
# Pick a range of commits
git cherry-pick a1b2c3d^..f4e5d6cWhen to use it
- A release manager cherry-picks a security patch commit from main onto the release/1.4 branch without pulling in unrelated features.
- A developer accidentally commits a fix to the wrong branch and cherry-picks it onto the correct one.
- A team backports a critical bug fix to two older support branches by cherry-picking the same commit SHA onto each.
More examples
Cherry-pick a single commit
Applies the exact changes from one commit onto the current branch, creating a new commit with the same diff.
# Find the commit hash on main
git log main --oneline
# e4a1f3c fix: patch SQL injection in search
# Apply it to the release branch
git switch release/1.4
git cherry-pick e4a1f3cCherry-pick a range of commits
The caret after the first hash makes the range inclusive of that starting commit, applying all commits in sequence.
# Apply commits from 9b0d14a up to and including e4a1f3c
git cherry-pick 9b0d14a^..e4a1f3cResolve a cherry-pick conflict
When the patch does not apply cleanly, you resolve the conflict manually, stage the file, and tell Git to finish applying.
git cherry-pick e4a1f3c
# CONFLICT in src/search.py
# Edit the conflict, then:
git add src/search.py
git cherry-pick --continue
# Or abandon the cherry-pick
# git cherry-pick --abort
Discussion