- Lab
- Core Tech

Guided: Cherry-Picking Git Commits
Cherry-picking is a crucial Git skill that is applicable to everyone's developer workflow. In fast-moving environments, bugfixes are being applied to production every day. The Git cherry-pick command will help you to selectively apply both bugfixes and features to production safely without hindering progress on your current feature branch. This Guided Code Lab will help you get up to speed with advanced cherry-picking techniques that will help you increase the speed in which you can apply bugfixes to production, alleviate the cost of context switching, and ease third-party integration of code in your current Git repo.

Path Info
Table of Contents
-
Challenge
### Introduction
The git
cherry-pick
command is a very useful command when it comes to managing more complex repositories. At its core, gitcherry-pick
takes specific commits from one branch and applies them to another branch. This is simple, but very effective! Some of the major benefits that cherry-picking can give you include:-
The ability to quickly apply bugfixes from one branch to another
-
Selective application of features
-
Safe integration with third-party changes to your repository
Throughout this Guided Code Lab, you are going to learn how to use git
cherry-pick
effectively in your development workflow. You are going to apply bugfixes, create features in an application, and use thecherry-pick
command to selectively apply these changes accordingly.Please feel free to reference the git cherry-pick documentation as you complete this Guided Code Lab.
Should you get stuck, you will find a
solution.txt
file within thesolution
directory on themaster
branch. This file details the commands to run in order to successfully complete each task.info> Changes made to this filesystem using Git may not automatically be conveyed in the filetree at times. If this happens, you can visualize changes to files by exiting out of the file and reopening it.
-
-
Challenge
### Selectively Applying Bugfixes
By far, the most useful and practical aspect of the git
cherry-pick
command is the ability it gives you to selectively apply bugfixes to the main branch. Often during development, you will find a bug while developing a new feature. By usingcherry-pick
, you can pick the commit that encapsulates the bugfix – applying this bugfix to the main branch so that production can get the fix quickly while you are still able to work on your new feature.In order to use git
cherry-pick
effectively, you must first become familiar with thegit log
command. Thegit log
command is very easy to use and lets you look back through your commit history on the current branch or on different branches. You are working on a feature within the current branch, but you just found a bug! You now must apply the bugfix in the current branch. As mentioned before, you can usegit log
to inspect the git history of not only the current branch but other branches. This lets you grab the commit SHA(s) that you need to apply commits via gitcherry-pick
. Now that you are on thedevelop
branch, it’s time tocherry-pick
the bugfix commit from your feature branch onto thedevelop
branch. This is a relatively common activity in git repository management. Well done! You now have a pretty firm grasp on both thegit log
command as well as the basics of thegit cherry-pick
command.In the next step, you'll dive a little deeper into cherry-picking!
-
Challenge
### Cherry-Picking Features
In the last step, you cherry-picked a bugfix from a feature branch onto the
develop
branch so that the production environment didn’t have to wait for the fix. Now it's time to learn how to selectively apply features from one branch to another.If you are working on a feature branch and the scope grows, sometimes it can make sense to use
cherry-pick
to apply a single feature from a set of features to your main branch. Similarly, you might be spiking out a new application workflow and you have a branch encompassing multiple features – in this case the gitcherry-pick
command has you covered too. The gitcherry-pick
command doesn’t just let you cherry-pick a single commit. This is good since not all bugfixes and features are completely encompassed within one commit. Fortunately, thecherry-pick
command has a convenient syntax for selecting a range of commits via a dot-dot syntax:..
. Nicely done! In this step, you learned more about the capabilities of thecherry-pick
command. In particular, you learned how to cherry-pick a range of commits and selectively apply individual features.In the next step, you'll look at cherry-picking and merge strategies.
-
Challenge
### Cherry-Picking and Merge Strategies
You’ve learned how to use git cherry-picking techniques to apply commits from one branch to another. Now you must also learn about some of the strategies you can use when your use of git
cherry-pick
creates conflicts. The gitcherry-pick
command lets you specify both the merge strategy and merge strategy options so that, if there are conflicts, you might not have to manage them manually! By default, thecherry-pick
command uses theort
merge strategy, Theort
merge strategy is a newer, more performant version of the very commonly usedrecursive
merge strategy you may already be familiar with. Therecursive
merge strategy used to be the default merge strategy that git used when pulling updates from a remote repository and/or merging a single branch into another branch.You can also, via the
-X
flag, specify a merge strategy option to many of the merge strategies that git provides. When using this flag, it is common to specify eithertheirs
orours
. Thetheirs
option tells git that if there are any conflicts that need resolution when merging, favor the incoming changes from their branch. It is a very common operation to want to bail out from acherry-pick
if there are too many or very messy conflicts. You can easily do this after usingcherry-pick
via the--abort
flag. You can also specify theours
merge strategy option when using therecursive
merge strategy. This option tells git to favor the current branches’ changes during conflict resolution should any conflicts arise during the merge. -
Challenge
### Conclusion
Well done! You just used all of the common git
cherry-pick
techniques that help to simplify complex git workflows. Throughout this lab, you learned how to selectively apply both bugfixes and features from one branch to another. You learned that thecherry-pick
command can cherry pick both singular and multiple commits and you learned how to manage merge conflicts that might arise from using gitcherry-pick
.The
cherry-pick
command is not for every situation though! Generally, it's a good idea to usecherry-pick
when you need to apply simple, singular commits from one branch to another. A very common example of a situation like this is when you need to apply a critical bugfix or patch created in a branch when that entire branch is not ready to be merged. There are also several guidelines to follow that will help you to not usecherry-pick
when you don’t need it:-
Do not favor cherry-picking over merges when the branch you are wanting changes from is ready in its entirety. The
cherry-pick
command is notorious for duplicating commits which many organizations do not like as it can pollute the git history of your repository. -
Try to avoid cherry-picking more than one commit in order to keep things as simple as possible.
-
Try to always avoid cherry-picking
merge
commits. The gitcherry-pick
command does offer a-m
flag which can help you choose what to do in this case.
From here, you can be confident when it comes to integrating cherry-picking into your specific Git workflow. You can continue your journey learning Git by pursuing both video courses and more Guided Code Labs here, at Pluralsight.
-
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.