Git: Picking Beautiful Cherries

Milan Brankovic
Apr 19, 2020 · 4 min read

git cherry-pick is a powerful command that enables arbitrary git commits to be picked by reference and appended to the current working HEAD. The command is typically used to selectively choose particular commit(s) from one branch within a repository onto a different branch. It applies the changes introduced by the named commit (identified by SHA1 reference) on the current branch and will introduce new, distinct commit. Strictly speaking, using git cherry-pick doesn’t alter the existing history within a repository; instead, it adds to the history. This command offers more fine-tuned selection than a general merge or rebase.

When to use cherry-pick

Maybe the backend team creates a data structure that the frontend team will also need to utilize. The frontend team could pick only the commit in which this hypothetical data structure was created. This pick would enable the frontend team to continue progress on their side of the project. Or

A critical bug is discovered and it is important to deliver a fix to end users as quickly as possible. One of the team members already fixed this bug while working on a new feature. The developer has created an explicit commit patching this bug. This new patch commit can be picked directly to hot-fix branch to fix the issue before it affects more users.

Example

[Fig. 1] starting point

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

$ git checkout master

Now we can apply the commit

$ git cherry-pick dbb6ffd

You can now verify that dbb6ffd has been applied to the master branch via the log listing. Note that dbb6ffd in the master has different SHA1 value, which is 907e9f4

[Fig. 2] after cherry-pick

Options

  • standardize commit message: When recording the commit, append a line that says “(cherry picked from commit …​)” to the original commit message in order to indicate which commit this change was cherry-picked from. It is better to use standardize commit message if we cherry-pick from a public branch.
$ git cherry-pick -x <commit-hash>

This will generate a standardized commit message. This way, your team can still keep track of the origin of the commit and may avoid merge conflicts in the future.

  • copy over the notes: Sometimes some of cherry-picked commits have notes which are not copied. Therefore, to bring them over as well, we will need to use:
$ git notes copy <from-commit-hash> <to-commit-hash>
  • cherry-pick multiple commits: We want to cherry-pick multiple commits, if they are linear, then only use the below command,
$ git cherry-pick <from-commit-hash>..<to-commit-hash>

To include <from-commit-hash> you will need to use caret symbol (^).

  • editing commit message: In some branches, the commit may be useful for different reasons. As a consequence, you may want to change the commit message when cherry-picking.
$ git cherry-pick -e <commit-hash>
  • moving without committing: The --no-commit option will execute the cherry pick but instead of making a new commit it will move the contents of the target commit into the working directory of the current branch.
$ git cherry-pick --no-commit <commit-hash>
  • adding signature: The --signoff option will add a 'signoff' signature line to the end of the cherry-pick commit message
$ git cherry-pick --signoff <commit-hash>

In addition to these helpful options git cherry-pick also accepts a variety of merge strategy options.

git cherry-pick also accepts option input for merge conflict resolution, which are: --abort, --continue, --skip and --quit.

The Startup

Get smarter at building your thing. Join The Startup’s +788K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Milan Brankovic

Written by

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Milan Brankovic

Written by

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store