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.
- master - This is the main branch i.e. our live/production servers point here
- develop - This is the branch where code gets pushed to, for integration testing on our test environments i.e. our testing servers point here
- 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
- 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
- Detailed explanation of the cherry pick command - https://www.devroom.io/2010/06/10/cherry-picking-specific-commits-from-another-branch/
- Super read for a git branching strategy - https://nvie.com/posts/a-successful-git-branching-model/
Top comments (0)