Fixing a Bug on Production with Cherry-Picking

Fixing a Bug on Production with Cherry-Picking

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:

  1. main branch: Production-ready code.
  2. feature-xyz branch: A work-in-progress feature.

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

  • Isolated bug fixes.
  • Minor features needed in another branch.
  • Back-porting changes to older branches.

Pro Tips

  • Avoid Overuse: Frequent cherry-picking can fragment your Git history, making it harder to track changes.

Mohd Danish Shaikh

Java developer @WIPRO

3 个月

Useful tips

要查看或添加评论,请登录

Ramanuj Das的更多文章

社区洞察

其他会员也浏览了