Everything you need as a full stack developer

Cherry-picking specific commits between branches

- Posted in VCS Version Control Systems by

TL;DR Cherry-picking allows fullstack developers to apply specific commits from one branch to another, without merging entire branches. This technique is essential when a critical bug needs to be fixed in production, but the feature branch can't be merged yet. To cherry-pick, identify the commit hash, checkout the target branch, and use Git's built-in cherry-pick command followed by the commit hash.

Cherry-Picking Specific Commits Between Branches: A Fullstack Developer's Best Friend

As a fullstack developer, you're no stranger to version control systems (VCS). You've likely spent countless hours wrestling with Git, SVN, or Mercurial, trying to tame the beast that is your codebase. But what happens when you need to cherry-pick specific commits from one branch and apply them to another? Fear not, dear developer, for today we're diving into the world of cherry-picking, and emerging victorious on the other side.

The Problem: Merging Mayhem

Imagine this scenario: you've been working on a feature branch, diligently crafting new functionality and squashing bugs. Meanwhile, your colleague has been working on a separate branch, refactoring some critical code. Both branches are crucial to the project's success, but they can't be merged just yet. That's when disaster strikes – a critical bug is discovered in production, and you need to apply a specific fix from the feature branch to the main branch, pronto.

Merging the entire feature branch into main isn't an option; it would introduce unnecessary changes and create a headache for your QA team. You need a way to pluck that one crucial commit from the feature branch and apply it to main, without disturbing the rest of the codebase. Enter cherry-picking, the hero we've all been waiting for.

Cherry-Picking 101

Cherry-picking is a Git technique that allows you to apply specific commits from one branch to another, while ignoring the rest of the commit history. It's like carefully selecting individual grapes from a bunch, rather than devouring the entire harvest.

To cherry-pick a commit, you'll need:

  1. The commit hash: Identify the specific commit you want to cherry-pick by finding its unique hash (e.g., git log or gitk --all).
  2. Checkout the target branch: Switch to the branch where you want to apply the cherry-picked commit (e.g., git checkout main).
  3. Cherry-pick the commit: Use Git's built-in cherry-pick command, followed by the commit hash (e.g., git cherry-pick <commit-hash>).

The Magic Happens

Once you've cherry-picked the commit, Git will create a new commit on your target branch, containing only the changes from the original commit. This new commit will have its own unique hash, and you can verify that it's been successfully applied by running git log or gitk --all.

Tips and Variations

  • Cherry-picking multiple commits: You can cherry-pick a range of commits using git cherry-pick <start-hash>..<end-hash>.
  • Resolving conflicts: If the cherry-picked commit introduces conflicts, Git will pause and allow you to resolve them. Once resolved, use git add and git cherry-pick --continue to complete the process.
  • Cherry-picking from a remote branch: You can cherry-pick commits from a remote branch by first fetching the remote branch (git fetch origin <remote-branch>) and then checking out the target branch before cherry-picking.

Conclusion

Cherry-picking specific commits between branches is an essential skill for any fullstack developer. By mastering this technique, you'll be able to navigate complex version control scenarios with ease, ensuring that your codebase remains organized and up-to-date. Remember, when faced with a tangled web of commits, take a deep breath, grab your cherry-picker, and pluck those commits like a pro!

Key Use Case

Here's a workflow/use-case example:

Urgent Bug Fix

Production is down due to a critical bug. The fix is already committed in the feature/new-login-system branch, but it can't be merged into main yet because of ongoing QA testing.

Step 1: Identify the commit Find the specific commit hash (e.g., git log) that fixed the bug in the feature/new-login-system branch.

Step 2: Checkout the target branch Switch to the main branch where you want to apply the cherry-picked commit (git checkout main).

Step 3: Cherry-pick the commit Use Git's built-in cherry-pick command, followed by the commit hash (git cherry-pick <commit-hash>), to apply only the bug fix to the main branch.

Verification Run git log or gitk --all to verify that the new commit has been successfully applied to the main branch.

Finally

When working with multiple branches, it's not uncommon for different teams or individuals to work on separate features or fixes simultaneously. In such scenarios, cherry-picking specific commits becomes a crucial tool in your Git arsenal, allowing you to selectively incorporate changes from one branch into another without disrupting the overall development workflow.

Recommended Books

• "Clean Code" by Robert C. Martin: A must-read for any developer looking to improve their coding skills and write better code. • "The Pragmatic Programmer" by Andrew Hunt and David Thomas: A classic in the field, offering practical advice and best practices for software development. • "Refactoring: Improving the Design of Existing Code" by Martin Fowler: A comprehensive guide to refactoring, helping you improve your codebase and write more maintainable code.

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more