Why I Love Cherry Pick Git Feature

in OCD11 months ago (edited)

Cherry-picking is a git feature which comes in handy in almost all the releases at least in my case when we have different branches like development, stage and prod.

PC: Pixabay.com

According to Coding Defined

Cherry picking in git means to choose a commit from one branch and apply it onto another. It is different than merge because cherry-pick will only allow one commit at a time whereas merge applies many commits onto another branch. An example can be when moving changes from the development environment to production environment you only want a certain commit to move rather than the whole branch so you will just cherry-pick the commit onto another branch.

But before telling you why I love cherry-pick, I should just warn you that if you don't know how to use it, you might end up messing the whole branch itself. The git cherry-pick command should be used by caution and thus you should use it only if you cannot use merge.

Why I love cherry-pick is because it does not disrupt your current working and thus you can push any commit to any branch. For example, last week I was working on a dev branch and it has like 10 commits which will eventually be moved to stage and production in like 1 week. But I got a request that one feature has to be deployed right away, and that feature I had worked on 2-3 days back and it had one commit. Now I cannot merge the whole development to master because then all the untested code will be pushed to the stage. So my option was to cherry-pick that individual commit from development and move it to stage without changing anything on either dev, stage or prod. And later when we test it in the stage we can merge the whole branch to prod.

I know I have not followed the one feature one branch principle in Git, which eventually will solve this issue. But this is how our organisation works, we commit everything directly on development and thus I did not have any other option than cherry-picking that commit. Also, cherry-pick can take more than one commit, so if my feature would have been distributed across multiple commits, there would be no problem in cherry-picking the commit.

There is a difference between merge and cherry-picking, the merge will take all the commits from a branch and merge it to the destination branch whereas cherry-pick will only take single commit or a number of commits of your choice.

And as I said I have used cherry-pick a lot of times when we have urgent requirement to push a commit from one branch to other because that's I guess the only way to move commit from one branch to other. Using Cherry Pick is also not that hard, and there are tons of articles on the internet which shows you how to do it. But be cautious and careful before doing the cherry-pick, its better to create a backup of your both source and destination branch before doing this, as this might corrupt or mess both the branch if not done properly. I have learned it hard way as when I have it for the first time, it did mess up a few things and thus I had to spend like one day to fix everything.