Cherry-pick
Last updated
The printed edition of Kusina comes from the release-1 branch. On main, Ana has fixed the leche flan's egg count, along with adding two new recipes that aren't ready to print. She wants just the fix on release-1. git cherry-pick copies one commit onto the branch you're on. In this lesson you'll use it, and see why it's for single commits, not whole features.
By the end of this lesson you can
- Copy one commit to another branch with git cherry-pick
- Explain that the copy is a new commit with a new hash
- Say when cherry-picking helps and when merging is better
Copying one commit
git cherry-pick <commit> takes the change that commit made, applies it to the branch you're on, and commits it with the same message and author. Like a one-commit rebase, the result is a new commit with a new hash; the original stays where it was.
$ git log --oneline --graph --all -5 * 57d7ab6 (HEAD -> main) Add bibingka recipe * 9e1cf42 Fix the egg count in leche flan * 25ae8fe Add halo-halo recipe * 67e435d (release-1, tag: v1.0) Cover the leche flan with foil * ba04ca2 Add lumpiang shanghai recipe
57d7ab6HEAD →main Add bibingka recipe9e1cf42Fix the egg count in leche flan25ae8feAdd halo-halo recipe67e435drelease-1⚑ v1.0 Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README
and 3 older commits
git cherry-pick -x <commit> adds a line "(cherry picked from commit …)" to the copy's message, so anyone can find the original. Useful whenever the original is in a shared branch.
If the change doesn't apply cleanly, you get a conflict: settle it, git add, git cherry-pick --continue (or --abort).
Fix the printed edition
- Find the egg-count fix’s hash on main with
git log --oneline main - On release-1, cherry-pick it with
-x - Check release-1 has 12 yolks but no halo-halo or bibingka
- Tag it
v1.0.1, a fix release
Working folder
no changes
Staging area
nothing staged
Repository
13 commits on main
You are in ~/kusina on main. release-1 is the branch the printed edition comes from.
Commit graph
57d7ab6HEAD →main Add bibingka recipe9e1cf42Fix the egg count in leche flan25ae8feAdd halo-halo recipe67e435drelease-1⚑ v1.0 Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
When it helps, and when it doesn't
Cherry-pick is for one commit (or a few) that one branch needs from another, when merging would bring too much:
- a fix that a release branch needs, as here;
- a commit made on the wrong branch, copied to the right one (then removed from the wrong one with reset, if it isn't shared);
- rescuing one good commit from an abandoned branch.
It isn't a way to bring a whole branch's work over. Git doesn't record that the copy and the original are the same change, so if the two branches are later merged, the change arrives twice (usually cleanly, sometimes as a conflict), and the history shows it twice.
Spot the trap: a feature, commit by commit
Joaquin’s photos branch has fifteen commits. Rather than open a pull request, he cherry-picks them onto main one by one.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-cherry-pick: applying a commit's change, -x, and conflicts
- Pro Git: Rebasing and Cherry-Picking Workflows: cherry-pick as a single-commit rebase