Part IV: Chapter 4.11

git cherry-pick

Copying a single commit to another branch.

Cherry-pick takes the diff introduced by any commit and re-applies it on top of your current branch — creating a new commit with the same changes but a different hash. It's a surgical tool: take exactly this one thing, leave everything else.

Pick Your Commits

Click any commit on the feature branch to select it, then cherry-pick it onto main. You can pick multiple. Watch the hash change on arrival.

main(HEAD)
9f8aInitial commit
c3d4Auth fix
e5f6Config update
feature← click a commit to select it

How It Works

Cherry-pick computes the diff between the picked commit and its parent — exactly what that commit changed. It then applies that diff on top of your current HEAD.

Because the parent commit is different, the resulting commit object has a different SHA-1. The changes are identical, but it is a genuinely new commit in the graph.

Cherry-pick can conflict just like a merge. If the code it's trying to patch no longer exists on the target branch, Git pauses for you to resolve the conflict, then git cherry-pick --continue.

When to Use It

  • 🍒Hotfix backports. You fixed a critical bug on main. Cherry-pick that fix onto the v2.0-stable release branch without merging all of main's newer commits.
  • 🍒Rescuing commits from a dead branch. Someone made a useful commit on a branch you're not merging. Cherry-pick the commit you want.
  • 🍒Moving a commit placed on the wrong branch. Committed to main instead of your feature branch? Cherry-pick it there, then revert it from main.

The Commands

Terminal
$# Cherry-pick a single commit:
$git cherry-pick j9k0
[main r7s8] Fix critical bug 🐛
1 file changed, 3 insertions(+), 1 deletion(-)
$# Cherry-pick a range (B..E means 'after B up to E'):
$git cherry-pick h7i8..n3o4
$# Cherry-pick without immediately committing (stage only):
$git cherry-pick --no-commit j9k0
# Changes are staged, letting you combine or inspect first.
$# Abort a cherry-pick in progress:
$git cherry-pick --abort