Git Cherry-Pick: Copy One Commit to Another Branch
Table of Contents
There comes a time in every developer's life when you want to yoink a commit from a branch, but you don't want to merge or rebase because you don't want all the commits. git cherry-pick solves this: it takes a single commit from anywhere in your repo and applies it to your current branch, no full merge required.
All the content from our Boot.dev courses are available for free here on the blog. This one is the "Cherry Pick" chapter of Learn Git 2. If you want to try the far more immersive version of the course, do check it out!
What Does Git Cherry-Pick Do?
Click to play video
The git cherry-pick command copies the changes from one specific commit and applies them as a new commit on your current branch:
git cherry-pick <commit-hash>
Say your history looks like this:
A - B - C main
\
D - E feature_branch
If you merge feature_branch into main, you get both D and E. But maybe E is half-finished garbage and you only want D. Cherry-pick it:
git switch main
git cherry-pick <hash-of-D>
A - B - C - D' main
\
D - E feature_branch
Notice that's D', not D. More on that in a second.
How to Cherry-Pick a Commit From Another Branch
- First, you need a clean working tree (no uncommitted changes). Commit your work or stash it.
- Identify the commit you want, typically by
git loging the branch it's on. - Switch to the branch you want the change on and run the cherry-pick.
Say you've got a branch called add_partners with two commits on it. The ClosedML commit is ready for main, but the WorldBanc one isn't:
git log add_partners --oneline
# f2b4a9c add WorldBanc to partners
# 8d3e7f1 add ClosedML to partners
git switch main
git cherry-pick 8d3e7f1
Run git log on main and you'll see the ClosedML commit sitting on top, while the WorldBanc commit stays behind on add_partners. If branches themselves are still fuzzy, read Git Branching first.
Cherry-Picked Commits Get New Hashes
A commit's hash is calculated from its contents and its parent. A cherry-pick applies the same diff onto a different parent, so Git creates a brand-new commit with a brand-new hash. The original commit still exists, untouched, on its own branch. (If you want the object-model details behind this, read Git Internals.)
This is the same reason commit hashes change after a rebase: same changes, different parent, different commit.
It also means cherry-picking is a copy, not a move. If you later merge the original branch anyway, the same change shows up as two different commits in your history, which is confusing at best.
Can You Cherry-Pick Multiple Commits?
Yep. Pass multiple hashes, or a range:
git cherry-pick 8d3e7f1 f2b4a9c
git cherry-pick 8d3e7f1^..f2b4a9c
Careful with ranges: A..B excludes A, while A^..B includes it.
There's also --no-commit, which applies the changes to your working tree and index without committing:
git cherry-pick --no-commit 8d3e7f1
That's handy when you want to grab changes from a few commits and squash them into a single new commit yourself.
What If a Cherry-Pick Hits a Conflict?
Cherry-picking replays a diff onto a branch that may have moved on, so conflicts happen just like they do with merge and rebase. Git pauses, you fix the conflicting files, git add them, and continue:
git cherry-pick --continue
Or bail out and pretend it never happened:
git cherry-pick --abort
The conflict markers and resolution process are identical to a merge, so if that's new territory, Git Merge Conflicts walks through it.
When Should You Actually Use Cherry-Pick?
Cherry-pick is great when you want one specific change without the rest of its branch:
- Backporting a bug fix from
mainonto a release branch. - Rescuing a good commit from a branch you're about to abandon.
- Grabbing a teammate's commit that you need right now, before their PR lands.
It's the wrong tool for routinely moving work between branches; that's what merge and rebase are for.
One more thing: cherry-pick copies a commit onto your branch, while git revert creates a new commit that undoes an existing one. It's basically a reverse cherry-pick.
If you're comfortable with the basics from Learn Git, the full Learn Git 2 course covers cherry-pick alongside reflog, bisect, worktrees, and the rest of the advanced toolkit.
Frequently Asked Questions
What does git cherry-pick do?
git cherry-pick copies the changes from a single commit and applies them as a new commit on your current branch, without merging or rebasing the whole branch.
Does git cherry-pick move or copy a commit?
It copies. The original commit stays on its branch, and the cherry-picked version is a new commit with a new hash because it has a different parent.
Can you cherry-pick multiple commits at once?
Yes. Pass multiple commit hashes to git cherry-pick, or a range like A^..B to apply every commit from A through B in order.
Do I need a clean working tree to cherry-pick?
Yes. Commit or stash any uncommitted changes before running git cherry-pick, otherwise Git will refuse or make a mess.
What happens if a cherry-pick has conflicts?
Git pauses the cherry-pick. Fix the conflicting files, git add them, then run git cherry-pick --continue. Or run git cherry-pick --abort to cancel.
