What does cherry-picking a commit with Git mean?
Recently, I have been asked to
cherry-pick a commit.
So what does cherry-picking a commit in git mean? How do you do it?
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
rebase which normally apply many commits onto another branch.
Make sure you are on the branch you want to apply the commit to.
git checkout master
Execute the following:
git cherry-pick <commit-hash>
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.
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>
Read more... Read less...
This quote is taken from; Version Control with Git (Really great book, I encourage you to buy it if you are interested in git)
Edit: Since this answer is still getting impression, I would like to add very nice in action video tutorial about it:
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, above
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.
Short example of situation, when you need cherry pick
Consider following scenario. You have two branches.
a) release1 - This branch is going to your customer, but there are still some bugs to be fixed.
b) master - Classic master branch, where you can for example add functionality for release2.
NOW: You fix something in release1. Of course you need this fix also in master. And that is a typical use-case for cherry picking. So cherry pick in this scenario means that you take a commit from release1 branch and include it into the master branch.
cherry-pick is a Git feature. If someone wants to Commit specific commits in one branch to a target branch, then cherry-pick is used.
git cherry-pick steps are as below.
- checkout (switch to) target branch.
git cherry-pick <commit id>
Here commit id is activity id of another branch.Eg.
git cherry-pick 9772dd546a3609b06f84b680340fb84c5463264f
- push to target branch
I prepared step-by-step illustrations what cherry-pick does — and an animation of these illustrations (near the end).
- Starting the command
git cherry-pick feature~2
feature~2is the 2nd commit before
feature, i.e. the commit
L' is from the user's point of view (commit = snapshot) the exact copy of the commit
Technically (internally), it's a new, different commit (because e.g.
L contains a pointer to
K (as its parent), while
L' contains a pointer to