← Git

Workflows & Best Practices

What does `git cherry-pick` do and when would you use it?

Show answer — try answering out loud first

git cherry-pick <commit> takes a specific commit from any branch and applies it onto the current branch, creating a copy of that commit (with a new hash). You use it when you want to bring over a single change (for example, a fix) without merging the whole branch it comes from.

Sometimes you don't want to integrate an entire branch, just one specific commit. For example: feature has 10 commits, but one fixes a critical bug that you need in main right now. Instead of merging all of feature (which isn't ready yet), you cherry-pick that single commit onto main.

How it works: cherry-pick reapplies that commit's changes onto your current branch as a new commit (same content, different hash). Conflicts can appear if the context is different, and they're resolved the same way as in a merge.

# I need the fix from commit abc1234 (which is on feature) here on main
git switch main
git cherry-pick abc1234
# A new commit with the changes from abc1234 is created on main

# Bring over several specific commits
git cherry-pick abc1234 def5678

# Bring over a range of commits (not including the first one)
git cherry-pick abc1234..def5678

# If there's a conflict: resolve it, run git add, and then:
git cherry-pick --continue

# If you change your mind during a conflict:
git cherry-pick --abort

Overusing cherry-pick to copy many commits between branches, when the right thing would be a merge or rebase; that duplicates commits (the same change with different hashes on two branches) and clutters the history, potentially causing conflicts when you merge later. Cherry-pick is for one-off cases, not for integrating entire branches.

"git cherry-pick takes a specific commit from another branch and applies it onto the current branch as a new commit, with a different hash. I use it to bring over a single change, like a critical fix, without having to merge the whole branch it comes from, which may not be ready yet. If there's a conflict, I resolve it, run git add, and git cherry-pick --continue. That said, I don't overuse it to copy many commits, because it duplicates changes and clutters the history; to integrate entire branches I use merge or rebase."

Quick challenge

On the feature branch (which isn't ready yet) there's a commit that fixes an urgent bug in production. How do you bring only that fix to main without merging all of feature?

See answer

With cherry-pick: you switch to main (git switch main) and run git cherry-pick <hash-del-fix>. That applies only that commit onto main as a new commit, without dragging along the rest of feature's commits, since it's not ready to be integrated yet.