Hotfix with Git Cherry-Pick

Hotfix with Git Cherry-Pick

The Issue

I must say that ever since I learned how to use Git, I no longer need to copy the project to a new folder for "backup" every time I'm about to make a major change. Back in the days when I didn't know Git, that was the method I could come up with to safeguard my code in case there was a bug and I needed to revert.

Git is a version control tool that I believe is highly effective for most programmers. One of Git's standout features is its ability to create commits, which you can think of as "commitments," for each line of code you add. Each of these commitments is recorded in the history and can be easily reviewed, allowing you to return your code to that state.

For each individual or organization, there are different ways to use Git, creating diversity in workflows. "Flow" is a term that denotes the work process in Git. The most common example is having a master branch as the main branch, a development branch for ongoing development, and branches with prefixes like feature/01, feature/02... These branches are created from the development branch for continuous deployment of new features.

"Hotfix" is also a prefix in branches used to fix a specific issue in a production environment. After releasing an application, there might be an unexpected issue that requires an immediate fix. Such issues are considered critical and need to be addressed as a top priority. Typically, the solution is to check out a hotfix branch from the master branch, for example, hotfix/01, fix the issue, test it, and if everything is fine, merge hotfix/01 into both the development and master branches.

That's in the case when we've just finished a release and discovered a bug, or at least when the time between discovering the bug and the changes on the development branch isn't too extensive compared to the master branch. Conversely, if the development branch has undergone too many changes due to continuous feature development by the team, then the possibility of conflicts arising when merging a hotfix into the development branch becomes a concern.

This situation is also understandable because the new features may have modified the code at a different location than the master branch. When the master branch is "outdated," and a hotfix is merged, conflicts are likely to occur. To address this scenario, we need a method called "cherry-pick" for hotfixes.

Git Cherry-Pick

git cherry-pick is a Git command used to apply one or more commits from one branch to your current branch. This allows you to apply specific changes from commits without having to merge the entire branch.

For example, if you have a commit on a latest development branch that resolves a specific issue you need to apply to your product branch, you can use git cherry-pick to apply that commit to your current branch.

git cherry pick

Normally, when merging one branch into another, Git will take all the changes from the commits to merge. However, with "cherry-pick," you can select a single commit to bring into your branch.

Applied to the hotfix scenario mentioned above, we create a hotfix branch from the development branch, make the necessary code changes, commit them, and then cherry-pick that commit into the master branch.

To explain further, because the development branch always contains code newer than or equal to the master branch, creating a hotfix from the development branch, making the changes, and only "picking" that commit into the master branch reduces the risk of conflicts.

In a broader context, cherry-pick is not limited to hotfixes; it can be used in various other situations whenever you need to "pick" a commit into a specific branch without merging the entire branch.

Conclusion

git cherry-pick is used to apply one or more commits from one branch to another. In this article, we are discussing the use of cherry-pick for hotfixes, especially when the development branch has diverged significantly from the master branch and contains many changes. Instead of merging a hotfix from the master into the development branch, which can lead to code conflicts, we create a hotfix from the development branch and cherry-pick the commit into the master branch, reducing the risk of conflicts.

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.
Author

Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.

Did you find this article helpful?
NoYes

Comments (0)

Leave a comment...