Hotfix with Git Cherry-Pick

Hotfix with Git Cherry-Pick

Daily short news for you
  • altcha-org/altcha is an open-source project that serves as an alternative to reCaptcha or hCaptcha.

    Studying these projects is quite interesting, as it allows you to learn how they work and how they prevent "spam" behavior 🤓

    » Read more
  • Manus has officially opened its doors to all users. For those who don't know, this is a reporting tool (making waves) similar to OpenAI's Deep Research. Each day, you get 300 free Credits for research. Each research session consumes Credits depending on the complexity of the request. Oh, and they seem to have a program giving away free Credits. I personally saw 2000 when I logged in.

    I tried it out and compared it with the same command I used before on Deep Research, and the content was completely different. Manus reports more like writing essays compared to OpenAI, which uses bullet points and tables.

    Oh, after signing up, you have to enter your phone number for verification; if there's an error, just wait until the next day and try again.

    » Read more
  • I just found a quite interesting website talking about the memorable milestones in the history of the global Internet: Internet Artifacts

    Just from 1977 - when the Internet was still in the lab - look how much the Internet has developed now 🫣

    » Read more

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.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

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

Comments (0)

Leave a comment...