Hotfix with Git Cherry-Pick

Hotfix with Git Cherry-Pick

Daily short news for you
  • Hic, I've been really busy lately, and there have been so many changes that I haven't had time to write a longer post. Instead, I'm trying to maintain this short daily update for readers. Since it's short, it's much quicker to write.

    I'm currently tweaking the interface a bit while also adding a new feature. Can you guess what it is? Here's a hint: it has something to do with AI and Tiktok 😁

    » Read more
  • Currently on the 14-day trial of Windsurf, today is day seven, and I have some quick impressions as follows:

    First, the interface is a bit more customizable, giving a flatter and friendlier feel compared to the traditional VSCode.

    Second, the suggestions are super fast but a bit hasty. They’re not always accurate, yet they confidently offer several lines at once. So, not every tab tab is correct. However, it reads context well, better than Copilot.

    Third, the Chat/Edit feature is top-notch, very good, almost a perfect understanding, probably on par with Cursor, but I’m not entirely sure; that's just how it feels.

    Additionally, one annoyance is that sometimes it suggests but the tab doesn’t match, which makes it a hassle to delete.

    I wonder how it will be after the 14 days, so I will continue to update. But overall, it’s way better than Copilot.

    Oh! One more thing, the Vietnamese in this one is terrible. I have no idea why!?

    » Read more
  • smee.io is a simple way to create a webhook address and map it to the localhost address on your computer.

    $ npm install --global smee-client $ smee -u https://smee.io/eu4UoW8vrKSZtTB

    » 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.
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...