Fixing a Bug on Production with Cherry-Picking
Ramanuj Das
Corporate Trainer, Google Certified Agile Coach | Microservices, Angular, React JS, Spring Boot, DevOps, Kubernetes, AWS
Think of a library with several bookshelves (branches). You’re preparing a display shelf (main branch) for a special event and want a specific book (commit) from the research shelf (feature-xyz). Instead of transferring the entire research shelf, you pick only the book you need and place it on display. That’s cherry-picking!
What is Cherry-Picking?
Cherry-picking is the process of selecting a specific commit from one branch and applying it to another. Unlike merging, which brings all the changes from a branch, cherry-picking lets you choose only what you need. It’s particularly useful for transferring isolated fixes or features.
Use Case: Fixing a Bug on Production
Imagine you’re working on a software project with the following branches:
While working on feature-xyz, you identify and fix a critical bug. The commit hash for this fix is abc123. Since this bug fix is urgently needed in main, but the rest of feature-xyz is incomplete, cherry-picking is the perfect solution.
Steps to Cherry-Pick a?Commit
1. Identify the?Commit
Use git log on the feature-xyz branch to locate the commit hash:
git log
Copy the hash of the bug-fix commit, e.g., abc123.
2. Switch to the Target?Branch
Move to the main branch:
领英推荐
git checkout main
3. Cherry-Pick the?Commit
Apply the specific commit to the main branch:
git cherry-pick abc123
4. Resolve Conflicts (if?any)
If conflicts arise, Git will pause the process. Resolve them manually, then continue:
git add .
git cherry-pick --continue
5. Push Changes to the?Remote
Once the commit is applied successfully, push it to the remote repository:
git push origin main
Cherry-Picking in Action: An?Analogy
Think of a library with several bookshelves (branches). You’re preparing a display shelf (main branch) for a special event and want a specific book (commit) from the research shelf (feature-xyz). Instead of transferring the entire research shelf, you pick only the book you need and place it on display. That’s cherry-picking!
When to Use Cherry-Picking
Pro Tips
Java developer @WIPRO
3 个月Useful tips