Rewriting history · lesson 6 of 6
Commands and ideas in this lesson
Agit cherry-pickcopy one commit onto this branch
B-xnote where the copy came from

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
  1. 57d7ab6 HEAD →main Add bibingka recipe
  2. 9e1cf42 Fix the egg count in leche flan
  3. 25ae8fe Add halo-halo recipe
  4. 67e435d release-1⚑ v1.0 Cover the leche flan with foil
  5. ba04ca2 Add lumpiang shanghai recipe
  6. 98a5edc Add a script that builds the index page
  7. d078487 Make the sinigang steps clearer
  8. 145dc1e Add pancit bihon recipe
  9. 52d1f4d Say how much salt goes in the adobo
  10. 1c17fad Add a README

and 3 older commits

1 of 3 release-1 is at v1.0. main has three newer commits; only the middle one, the egg fix, should be printed.

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

Git sandbox
  1. Find the egg-count fix’s hash on main with git log --oneline main
  2. On release-1, cherry-pick it with -x
  3. Check release-1 has 12 yolks but no halo-halo or bibingka
  4. 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

  1. 57d7ab6 HEAD →main Add bibingka recipe
  2. 9e1cf42 Fix the egg count in leche flan
  3. 25ae8fe Add halo-halo recipe
  4. 67e435d release-1⚑ v1.0 Cover the leche flan with foil
  5. ba04ca2 Add lumpiang shanghai recipe
  6. 98a5edc Add a script that builds the index page
  7. d078487 Make the sinigang steps clearer
  8. 145dc1e Add pancit bihon recipe
  9. 52d1f4d Say how much salt goes in the adobo
  10. 1c17fad Add a README
  11. 9c53888 Add leche flan recipe
  12. 1f359a2 Add sinigang recipe
  13. 601c654 Add 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

Spot the trapJoaquin

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

After git cherry-pick abc1234, does the new commit have hash abc1234?
When is cherry-pick the right tool?

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.

All sources for Simple Git