The Power of Cherry-Picking in Git
Streamline Your Development Process with Precise Code Selection
Table of contents
Cherry-picking in Git is like selecting the best pieces of code from different branches and merging them.
π Whether it's fixing a bug π, implementing a new feature π οΈ, or improving performance β‘, cherry-picking allows developers to handpick the most valuable changes and apply them where they're needed most.
What is Cherry-Picking?
Imagine your software project as a garden of code, with each branch representing a different plantβsome growing new features, others fixing bugs. Now, cherry-picking is like plucking the ripest, juiciest cherries from different plants and combining them into a delicious fruit salad. In GitHub terms, it's about selecting specific changes (commits) from one branch and applying them to another, without bringing along everything else.
Why Does It Matter?
Suppose your team is working on a big project with multiple branches. One branch has a critical bug fixπ, while another has a cool new featureπ οΈ. Instead of merging entire branches and risking conflicts or unintended changes, cherry-picking allows you to grab only the essential commits and apply them exactly where they're needed.π
How Does It Work?
Let's break it down with a simple example:π
Let's say we have two branches : feature/login
and feature/ui
.
In feature/login
, there's a commit that introduces a bug fix for the login system:
#if you want to check the commits just use "git log" command
Commit: 1234567
Changes:
- Fixed login validation issue
- Updated error message
In feature/ui
, in this branch, we are working on new stuff.
Commit: abcdefg
Changes:
- Refactored CSS for better responsiveness
- Added new icons for buttons
- user profile section design
Commit: hfgsfvah
Changes:
- dashboard section ui
- staff profile css work
Now, we realize that the bug fix in feature/login
is critical, but we also want to incorporate the UI improvements from feature/ui
without bringing in unrelated changes.
Using cherry-picking, we can do:π
# Checkout to the feature/login branch
git checkout feature/login
# Cherry-pick the commit with the UI improvements from feature/ui branch
git cherry-pick abcdefg
This will apply only the changes related to UI improvements from the feature/ui
branch onto the feature/login
branch, without including any other changes.
Thanks a lot for reading till the end π If you liked the article, please give likes and share it with your others.
Email: atul19251@gmail.com
LinkedIn: https://www.linkedin.com/in/atul-kumar-singh-673357102/