What Cherry Picking Means in Testing
Cherry Picking - gathering cherries - is the process of choosing, from the features
that have already been built, the ones that will go into the nearest release.
When does this happen: when the developers have 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:
- Bug hunting becomes harder - more new features means it is harder to figure things out
- The testing process may drag on - simply because of the volume
- There may not be enough features left for the next release - for example, the holiday period begins and the PM knows that far less will be done over the next sprint
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 lets you pick arbitrary
Git commits by reference and add them to the current working head.
Cherry picking is the act of picking a commit from one branch and applying it to another.
git cherry-pick can be useful for undoing changes.
For example, suppose a commit was accidentally made to the wrong branch.
You can switch to the right branch and pick the commit where it should be.
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 are preferable instead.
That said, git cherry-pick is a handy tool for several scenarios...
Team collaboration.
Often a team has individual members working on 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 a backend developer creates a data structure that the frontend
will also need to use.
The frontend developer can use git cherry-pick to pick the commit
in which this hypothetical data structure was created.
This pick lets the frontend developer get on with 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 uncover an already existing bug.
The developer creates a separate commit that fixes this bug.
This new patch commit can be picked directly into the main branch,
to fix the bug before it affects more users.
Undoing changes and recovering lost commits
Sometimes a feature branch can become stale and never merge into master.
Sometimes a pull request can be closed without being merged.
Git never loses those commits, and with commands such as git log and git reflog
you can find them and bring them back to life.
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 the commit reference.
The commit reference you need
can be found with git log.
In this example we want to cherry-pick commit `f` into master.
First we make sure we are working on master.
git checkout master
Then we perform the cherry-pick with the following command:
git cherry-pick f
After that, 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 run with various options:
-edit
Passing the -edit option will cause git to ask for a commit message before applying the cherry-pick operation
--no-commit
The --no-commit option will perform the cherry pick, but instead of making a new commit, it will move the content 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 options.
Read more about these parameters in the git merge strategies documentation.
Besides, 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 handy command that is incredibly useful in several scenarios. cherry-pick should not be misused in place of git merge or git rebase. The Git log command will certainly help you track down the commits you need.
Useful software and other materials
Article author: Andrei Olegovich