Cherry-Picking Commits
git cherry-pick copies a single commit from one branch onto another. It's the tool for "I need that one fix over here, but not the rest of the branch" — for example, bringing an urgent bug fix into a release branch.
Picking one commit
The redesign branch isn't ready, but it contains a bug fix that customers need now:
git log --oneline main..redesign
0ff91ab (redesign) Switch theme to green 02a18be Fix shipping rounding error a85e9c3 Try a blue product theme
On the branch that should receive the fix, cherry-pick the commit by its hash:
git cherry-pick redesign~1
[main 2c11805] Fix shipping rounding error Date: Mon Mar 2 09:35:00 2026 +0000 1 file changed, 1 insertion(+), 1 deletion(-)
git log --oneline cat shipping.txt
2c11805 (HEAD -> main) Fix shipping rounding error fd92f1f Create product page Shipping: 4.99
main now has the fix and nothing else from the redesign. The copy is a new commit with its own hash, because it has a different parent; the original stays on redesign.
Recording where it came from
-x adds a line to the message saying which commit was copied. This is useful on public branches, so readers can trace the fix back:
git cherry-pick -x redesign~1 git log -1 --format=%B
[main fd9b253] Fix shipping rounding error Date: Mon Mar 2 09:35:00 2026 +0000 1 file changed, 1 insertion(+), 1 deletion(-) Fix shipping rounding error (cherry picked from commit 02a18bee345765196b06fe7fc380f1540d6c6c8c)
Several commits
List several hashes, or use a range. A..B picks the commits after A up to B; A^..B includes A itself:
git cherry-pick a1b2c3d f4e5d6c git cherry-pick main~3..feature
If you'd rather combine them before committing, add -n (--no-commit): the changes are applied and staged, and you commit once yourself.
Conflicts
If the commit changes lines that are different on the target branch, the cherry-pick stops with a conflict. Resolve and git add as usual, then:
| Command | Effect |
|---|---|
git cherry-pick --continue | Commit the resolved result and move on |
git cherry-pick --skip | Skip this commit (when picking several) |
git cherry-pick --abort | Cancel and return to where you started |
git cherry-pick redesign
Auto-merging product.txt CONFLICT (content): Merge conflict in product.txt error: could not apply 0ff91ab... Switch theme to green hint: After resolving the conflicts, mark them with hint: "git add/rm <pathspec>", then run hint: "git cherry-pick --continue". hint: You can instead skip this commit with "git cherry-pick --skip". hint: To abort and get back to the state before "git cherry-pick", hint: run "git cherry-pick --abort".
git cherry-pick --abort git status -s
When to use it — and when not
- Good uses: backporting a fix to a release branch; rescuing a commit made on the wrong branch; picking a useful commit out of an abandoned branch.
- Not a replacement for merging. If you'll merge the whole branch later anyway, cherry-picking first creates duplicate commits with the same change, which clutters history and can cause confusing conflicts. Merge or rebase instead.
Moving a commit to the right branch
A classic slip: you committed on main but meant to start a feature branch. Cherry-pick isn't even needed — create the branch where you are, then move main back:
git branch feature/oops # the new branch keeps the commit git reset --hard HEAD~1 # main goes back one commit git switch feature/oops
Try it yourself
Make a branch with three commits that change three different files. On main, cherry-pick only the middle one with -x, and check with git log that its message mentions the original hash.