Skip to content Skip to navigation

Site Retired

As of May, 2022, this website is no longer updated and has been replaced with a static copy.

Cherry Picking - Small Git lesson

Small commits allow for big wins.

Something that I have been using a lot lately is GIT's cherry-pick command. I find the command very useful, and it saves me bunches of time. Here is a quick lesson on what it does and an example use case.

What is GIT cherry-pick? man page

Git cherry-pick allows you to merge a single commit from one branch into another.  To use the cherry-pick command follow these steps:

  1. Check out the branch into which you want to merge the commit. (E.g.: git checkout master)
  2. Identify the commit hash through your favorite method. You can use git log, a GUI tool such as sourcetree or tower, or if you use GitHub or BitBucket you can use their interface. In SWS we use GitHub, so I tend to use that method often. With GitHub you can find the commit hash on the commit list page or on the individual commit page itself.  See the screenshots below.
  3. Pick'em! eg: git cherry-pick 9638d99c89a92350ad2f205f47af24b81ac39a64
  4. If there is a merge conflict you will have to resolve that with your favorite merge tool, but otherwise you have now successfully pulled in the work from the other branch. 

Why would I use this?

That is a good question. Why would you cherry-pick instead of just merging one branch into the other? The short answer is that you cherry-pick when a full branch merge is not possible due to incompatible versions or simply because you do not want everything in the other branch. 

A common use case for Drupal module maintainers is when a security vulnerability has been identified and fixed in a Drupal version number and has to be applied to others. For example, if a security fix has been found in the Drupal 6 version of the stanford_events module, then that fix may apply to the Drupal 7 version. Instead of having the developer apply the change manually to the Drupal 7 version that developer can use Git cherry-pick for commit and carry on. 

How to find the commit hash on GitHub: