Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

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.

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 40m
Published
Clock icon Mar 01, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    ### Introduction

    The git cherry-pick command is a very useful command when it comes to managing more complex repositories. At its core, git cherry-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 the cherry-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 the solution directory on the master 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.

  2. 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 using cherry-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 the git log command. The git 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 use git 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 git cherry-pick. Now that you are on the develop branch, it’s time to cherry-pick the bugfix commit from your feature branch onto the develop branch. This is a relatively common activity in git repository management. Well done! You now have a pretty firm grasp on both the git log command as well as the basics of the git cherry-pick command.

    In the next step, you'll dive a little deeper into cherry-picking!

  3. 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 git cherry-pick command has you covered too. The git cherry-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, the cherry-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 the cherry-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.

  4. 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 git cherry-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, the cherry-pick command uses the ort merge strategy, The ort merge strategy is a newer, more performant version of the very commonly used recursive merge strategy you may already be familiar with. The recursive 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 either theirs or ours. The theirs 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 a cherry-pick if there are too many or very messy conflicts. You can easily do this after using cherry-pick via the --abort flag. You can also specify the ours merge strategy option when using the recursive merge strategy. This option tells git to favor the current branches’ changes during conflict resolution should any conflicts arise during the merge.

  5. 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 the cherry-pick command can cherry pick both singular and multiple commits and you learned how to manage merge conflicts that might arise from using git cherry-pick.

    The cherry-pick command is not for every situation though! Generally, it's a good idea to use cherry-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 use cherry-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 git cherry-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.

Zach is currently a Senior Software Engineer at VMware where he uses tools such as Python, Docker, Node, and Angular along with various Machine Learning and Data Science techniques/principles. Prior to his current role, Zach worked on submarine software and has a passion for GIS programming along with open-source software.

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.