I needed to pull a specific commit from another branch without merging the whole thing. A bug fix on develop that needed to go to main immediately, but the rest of develop wasn’t ready.

Basic usage Link to heading

Cherry-pick a commit:

git cherry-pick abc123

If there are conflicts, resolve them and continue:

git cherry-pick --continue

Or abort:

git cherry-pick --abort

Cherry-pick multiple commits:

git cherry-pick abc123 def456

If you want to edit the commit message:

git cherry-pick abc123 -e

When to use cherry-pick Link to heading

Cherry-pick is the right choice when:

  • Hotfixes - A fix on develop needs to go to main/production immediately
  • Backporting - A feature from main needs to go to a release branch
  • Selective merging - You only want specific commits from a feature branch

Don’t use cherry-pick for regular merging. If you need most commits from a branch, just merge it.

The duplicate commit problem Link to heading

Cherry-pick creates a new commit with a different SHA. If you later merge the original branch, you’ll have two commits doing the same thing. Git usually handles this fine, but it can cause confusion and occasional merge conflicts.

To avoid this:

  • Cherry-pick to short-lived branches (like release branches)
  • Delete the original branch after cherry-picking if it won’t be merged
  • Consider git rebase if you need to move a whole series of commits

Undoing a cherry-pick Link to heading

If things go wrong:

git reset --hard HEAD~1

That undoes the last commit. Be careful - it throws away changes.

If you need to undo commits on shared branches, git revert is the safer option.

Further reading Link to heading