Cherry-pick: copy one commit
Take exactly the commit you need — and nothing else.
1 Learn the idea
Sometimes a branch contains one commit you need right now — say a bug fix buried in a half-finished feature branch. Merging would drag in everything else. git cherry-pick copies just that one commit onto the branch you are on.
What actually happens
- Git looks at the chosen commit and works out what it changed compared to its parent.
- It applies those same changes on top of
HEAD. - It creates a brand-new commit with the same message — but a different hash, because it has a different parent.
2 Watch it happen
Press Play or use → to step through. Watch the graph as each command runs.
Read this demo as text
$ git log --oneline --all
The
featurebranch has three commits. The middle one fixes a crash that is also hurting users onmaintoday. The search feature is not ready yet.$ git switch main
We are on
main, the branch that should receive the fix. Cherry-pick always copies onto the branch you are on.$ git cherry-pick feature~1
feature~1means "one commit before the tip of feature" — the crash fix. Git replays its change on top ofmainand makes a new commit.$ git log --oneline --all
Look closely: the fix now exists twice, with the same message but different hashes. The original on
featureis untouched.$ ls
Only
app.jsgot the fix.search.jsandresults.jsdid not come along — that is the whole point.$ git show HEAD
git showconfirms the new commit contains exactly the same change as the original.$ git merge feature
Later, when the feature is done, merging still works: git sees both sides made the identical change to
app.js, so there is nothing to argue about.
- Brings in every commit of the branch
- Keeps the original commits
- Branches stay connected in the graph
- Brings in exactly one commit
- Creates a copy with a new hash
- No connection between the branches
git cherry-pick <commit>Copy one commit onto the current branchgit cherry-pick feature~1Refer to commits relative to a branch tipgit cherry-pick --continueFinish after fixing a conflictgit cherry-pick --abortGive up and return to where you were3 Check your understanding
You cherry-pick commit a1b2c3d from feature onto main. What is the hash of the new commit on main?
In this graph, you are on main. Which command brings only "Fix typo in footer" onto main?
A cherry-pick stops with a CONFLICT. What is the right sequence to finish it?
When is merge the better choice over cherry-pick?
4 Practice for real
Grab just the fix
The experiment branch has a real bug fix ("Fix login bug") sandwiched between two wild experiments. Get only the login fix onto main. The experiments must not reach main, and experiment must stay as it is.