DEV Community

Cover image for Git cherry pick command explained
Shuhaid Lambe for Swipez

Posted on

Git cherry pick command explained

The cherry-pick command allows you to integrate individual commits from any branch into your current branch.

To set some context, here is a quick overview of our git branching structure.

  1. master - This is the main branch i.e. our live/production servers point here
  2. develop - This is the branch where code gets pushed to, for integration testing on our test environments i.e. our testing servers point here
  3. release-x.x.x - These branches are cut for any long running releases or features we are working on as a team. The release branch when ready is merged to the develop branch to be tested in the integrated test environment. Post which the release branch is merged with master when it's ready to be pushed live
  4. hotfix-x.x.x - Arrgh! That nasty little bug sneaked through to live 😑. Create a hotfix branch from master, quash the bug in this hotfix branch. Then merge hotfix branch to develop and master branches to push your hotfix in to your test and live environments.

Cherry pick command

Consider the scenario you have cut a release branch to work on a feature. In the midst of that you need to make a small change to your live environment. Or even worse you have a bug that has sneaked through to your live environment and needs to be fixed asap! It does not make sense to wait and bundle it with your release branch as that is still 2 weeks from completion. In this scenario you will create a hotfix branch from your master branch. Make your changes in the hotfix branch and merge it back to the develop and master branch once you are done.

You might not be ready to merge your release branch with the develop or master branch yet as it might have breaking changes that you do not want to integrate yet. But you only need the hotfix commit in your release branch and that's when the cherry-pick command comes in handy.

git checkout release-x.x.x
git cherry-pick 47rul5
# 47rul5 is the reference for the hotfix commit

Check out links in the further reading section below, to get more in depth understanding on using the git cherry-pick command. It is a useful tool to have in your git commands arsenal.

Further reading

Photo by T. Q. on Unsplash

Top comments (0)