Git Cherry Pick

Git cherry-pick image from www.aredel.com
Cherry-pick
Freepick

Introduction

In this article you can learn about using the git cherry-pick command.

First general considerations are given, then information specifically about the command git cherry-pick

General information

Cherry Picking - picking cherries - is the process of choosing from the implemented features those that will go into the nearest release.

When this happens: in the case when the developers managed to create a large number of new features, but management believes that it is not worth rolling out all of them at once.

Why not release all the features at once:

API testing image from www.aredel.com
Photo: freepik.com

git cherry-pick

In git there is a command

git cherry-pick

You can read the specification here

git cherry-pick is a powerful command that allows you to select arbitrary Git commits by reference and add them to the current working head.

In simple words: cherry-pick copies certain commits from one branch to another. That is, instead of a full merge of the branch into master, you can add only some commits

The commit that is copied is not removed from the original branch - this is the difference from picking real cherries, which, of course, do not stay on the branch.

git cherry-pick can be useful for undoing changes.

For example, suppose a commit was accidentally made in the wrong branch.

You can switch to the correct branch and cherry-pick it there.

If you cherry-pick and then merge the branch it was made from - a duplicate of the cherry-picked commit will appear.

This can cause confusion if not everyone is aware of what is happening.

When to use git cherry pick

git cherry-pick is a useful tool, but not always the best practice.

Cherry picking can lead to duplicate commits, and in many scenarios where cherry picking would work, traditional merges (merge) are preferable instead.

That said, git cherry-pick is a convenient tool for several scenarios...

Team collaboration.

Often a team has individual members working in or around the same code.

Perhaps a new product feature has a backend and a frontend component.

Perhaps there is some shared code between two areas of the product.

Perhaps the backend developer creates a data structure that the frontend will also need to use.

The frontend developer can use git cherry-pick to select the commit in which this hypothetical data structure was created.

This selection will allow the frontend developer to continue working on their part of the project.

Bug fixes

When a bug is found, it is important to deliver the fix to end users as quickly as possible.

For example, suppose a developer has started work on a new feature.

While developing this new feature they discover an already existing bug.

The developer makes a commit with a fix for this bug.

This new commit with the patch can be added directly to master, to fix the bug before it affects more users.

A situation may also arise when a bug is found in a later version of the product, but the fix needs to be added to several previous versions that are still in use.

Undoing changes and using commits from already inactive branches

Sometimes a feature branch may become stale and never merge into master.

Sometimes a pull request may be closed without merging.

Git never loses these commits, and with commands such as git log and git reflog, they can be found and brought back to life.

Git cherry-pick image from www.aredel.com
Cherry-pick
Freepick

How to use git cherry pick

To demonstrate how to use git cherry-pick, suppose we have a repository with the following branch state:

a - b - c - d Master \ e - f - g Feature

using git cherry-pick is straightforward and can be done as follows:

git cherry-pick commitSha

In this example commitSha is a commit reference.

The required commit can be found with git log.

git log --oneline

The log of another branch can be examined with

git log branch_name --oneline

In this example we want to cherry-pick commit `f` into master.

First we make sure that we are working on master.

git checkout master

Then we perform the cherry-pick with the following command:

git cherry-pick f

After execution our git history will look like this:

a - b - c - d - f Master \ e - f - g Feature

Commit f was successfully cherry-picked into the main branch

git cherry pick examples

git cherry pick can also be performed with various options:

-edit

Passing the -edit option will cause git to request a commit message before applying the cherry-pick operation

--no-commit

The --no-commit option will perform the cherry picking, but instead of making a new commit, it will move the contents of the target into the working directory of the current branch.

--singoff

The --singoff option from the system will add a 'singoff' signature line to the end of the cherry-pick commit message

In addition to these useful options, git cherry-pick also accepts various merge strategy variants.

Read more about these parameters in the git merge strategies documentation.

In addition, git cherry-pick also accepts options for resolving merge conflicts, including the options: --abort --continue and --quit; these options are covered in more detail in relation to git merge and git rebase.

Summary

cherry-pick is a powerful and convenient command that is incredibly useful in several scenarios. cherry-pick should not be misused instead of git merge or git rebase. The Git log command will definitely help you find the cream.

Article author: Andrei Olegovich