Categories
cherry-pick git git-cherry-pick

What does cherry-picking a commit with Git mean?

3011

What does git cherry-pick <commit> do?

2

  • 20

    Instead of merge, with cherry-picking re-committing from a branch to the target branch (ex: master) is easier.

    Jan 9, 2016 at 14:29

  • 1

    Could one say?: “Cherry-picking a commit means creating a temporary branch on HEAD, merging the diff of that commit into it, then fast-forward HEAD.” Or in simple words: “Merging a single commit“.

    – U. Windl

    Apr 15, 2021 at 11:53

3641

Cherry picking in Git means to choose a commit from one branch and apply it onto another.

This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch.

  1. Make sure you are on the branch you want to apply the commit to.

     git switch master
    
  2. Execute the following:

     git cherry-pick <commit-hash>
    

N.B.:

  1. If you cherry-pick from a public branch, you should consider using

     git cherry-pick -x <commit-hash>
    

    This will generate a standardized commit message. This way, you (and your co-workers) can still keep track of the origin of the commit and may avoid merge conflicts in the future.

  2. If you have notes attached to the commit they do not follow the cherry-pick. To bring them over as well, You have to use:

     git notes copy <from> <to>
    

Additional links:

9

  • 5

    Is cherry picking really necessary? Won’t a mixed reset or a soft reset do a similar job?

    – Nav

    Jun 6, 2014 at 9:13

  • 8

    git push is last step to take changes on master

    Apr 3, 2017 at 10:30

  • 110

    FYI: A commit semantically contains all the files of the working tree of that moment (and the commit hash of the previous commit), so you are not applying a whole commit to another commit, but the changes a commit did on the previous commit "cherry-pick commit applies the changes introduced by the named commit on the current branch" Most ppl tend to think of commit as changes (like svn was iirc), but it is not, each commit refers to the complete working tree. Though this doesn’t make a difference in this case, it can help in understanding why git works like it does.

    Jul 3, 2017 at 11:18

  • 2

    @Zitrax are notes different from commit message? My single git cherry-pick command was able to bring over my commit message as well. Are you talking about something else? I didn’t need to run git notes command at all to accomplish it.

    – RBT

    Aug 15, 2017 at 5:09


  • @RBT Yes notes are different from the commit message, the commit message follows the commit on cherry-pick. See git-scm.com/docs/git-notes

    – Zitrax

    Aug 15, 2017 at 11:04

425

This quote is taken from: Version Control with Git

Using git cherry-pick The command git cherry-pick commit applies the
changes introduced by the named commit on the current branch. It will
introduce a new, distinct commit. Strictly speaking, using git
cherry-pick doesn’t alter the existing history within a repository;
instead, it adds to the history. As with other Git operations that
introduce changes via the process of applying a diff, you may need to
resolve conflicts to fully apply the changes from the given commit
.
The command git cherry-pick is typically used to introduce particular
commits from one branch within a repository onto a different branch. A
common use is to forward- or back-port commits from a maintenance
branch to a development branch.

$ git checkout rel_2.3
$ git cherry-pick dev~2 # commit F, below

before:
before

after:
after

Also, here is a very nice in action video tutorial about it: Youtube: Introduction to Git cherry-pick

12

  • 18

    when cherry-picked commits are taken on some branch (b1) and later delivered to master. And if branch b1 (from which commits were originally picked) is also tried to be delivered to master. How about the conflicts? Is that taken care or how does it work?

    – parasrish

    Jun 28, 2016 at 6:02

  • 5

    @parasrish Yes, they are already taken care with your previous merges. So you did changes a,b,c,d from (b1) branch. You cherry picked only “c”. Then in future once you merge from (b1) to master, since “c” changes are same, it will only merge a,b,d and remain “c” changes. But if you rollback your merge, then you will go back changes with “c” in it. You will need to roll them back separately.

    Nov 22, 2016 at 16:18


  • 29

    It should be emphasized: In the example as given, only the difference (F – E) is applied to Z. That is a narrow case. Cherry-pick may be used to apply the differences of multiple commits, say, all of the differences between two non-adjacent commits. For example, following from above, (F – E), (E – D), (D – C), and (C – B). That is equivalent to applying the difference (F – B).

    Jul 25, 2017 at 21:28

  • 2

    Also, what happens if the selected Commit (F in the example) has more than one immediate predecessor?

    Jul 25, 2017 at 21:29

  • 4

    @j2emanue in other words, cherry-pick will only take changes of last-commit. If you commit 3 different times, and if you cherry pick last one, it will not take changes on first and second commit. Merge command will take all your changes and apply to your target (master) branch.

    May 14, 2018 at 14:26


189

Cherry picking in Git is designed to apply some commit from one branch into another branch. It can be done if you eg. made a mistake and committed a change into wrong branch, but do not want to merge the whole branch. You can just eg. revert the commit and cherry-pick it on another branch.

To use it, you just need git cherry-pick hash, where hash is a commit hash from other branch.

For full procedure see: http://technosophos.com/2009/12/04/git-cherry-picking-move-small-code-patches-across-branches.html

1

  • What happens if the underlying branch changed, such that the cherry pick doesn’t make sense?

    Feb 11 at 19:24