DEV Community

Cover image for Cherry-Picking in Git: A Solution for Pull Request Discrepancies
Odumosu Matthew
Odumosu Matthew

Posted on

Cherry-Picking in Git: A Solution for Pull Request Discrepancies

In the world of professional software development, teams rely on Git to manage their version control systems. However, not all processes go smoothly. At times, discrepancies arise between pull requests and the remote branch, leading to situations where merging the branch into production is not feasible. This is where cherry-picking comes to the rescue.

This article explores cherry-picking, its significance, real-life scenarios that warrant its use, and the commands and steps to implement it effectively.

What is Cherry-Picking?

Cherry-picking in Git refers to the process of selecting specific commits from one branch and applying them to another branch. Unlike merging or rebasing, which operate on the entire branch, cherry-picking allows developers to isolate and apply only the changes they need.

Real-Life Scenarios That Warrant Cherry-Picking

  1. Critical Bug Fix During a Release: Imagine your team is working on a feature branch while preparing for a production release. During testing, a critical bug is identified and fixed in the development branch. However, the development branch contains untested or incomplete features, making it unsuitable for merging into production. Cherry-picking allows you to apply only the bug fix to the production branch.

  2. Selective Backporting of Changes: Your application supports multiple versions (e.g., v1.0, v2.0, and v3.0). A security vulnerability is patched in the main branch for v3.0, and you need to backport this fix to the older branches (v1.0 and v2.0). Cherry-picking ensures that the critical fix is applied without introducing unnecessary changes.

  3. Resolving Pull Request Discrepancies: If multiple pull requests modify the same files and create conflicts in the main branch, merging becomes difficult. By cherry-picking, you can manually apply selected changes from conflicting pull requests to streamline the production process.

  4. Isolated Hotfix Deployment: While working on a new feature, a customer-reported issue requires an immediate hotfix. Instead of disrupting the ongoing development process, you can fix the issue in a separate branch and cherry-pick the commit into production.

Step-by-Step Guide to Cherry-Picking

1. Identifying the Commit to Cherry-Pick

The first step is to identify the commit you want to cherry-pick. Use the git log command to list the commit history:

git log --oneline

Enter fullscreen mode Exit fullscreen mode

This will display commit hashes and messages. Note down the hash of the commit you need (e.g., abc123).

2. Switch to the Target Branch

Ensure you're on the branch where you want to apply the selected commit. Use the following command to switch branches:

git checkout production

Enter fullscreen mode Exit fullscreen mode

3. Perform Cherry-Picking

To apply the selected commit to your current branch, use:

git cherry-pick <commit-hash>

Enter fullscreen mode Exit fullscreen mode

For example:

git cherry-pick abc123

Enter fullscreen mode Exit fullscreen mode

This applies the changes from abc123 to your production branch.

4. Resolving Merge Conflicts (If Any)

Conflicts can occur if the changes in the selected commit overlap with the code in the target branch. In such cases, Git will pause the cherry-pick process and prompt you to resolve conflicts. Follow these steps:

  • Open the conflicting files and resolve the conflicts manually.

  • Stage the resolved files:

git add <file-path>

Enter fullscreen mode Exit fullscreen mode
  • Continue the cherry-pick process:
git cherry-pick --continue

Enter fullscreen mode Exit fullscreen mode
  • f you want to abort the cherry-pick process instead:
git cherry-pick --abort

Enter fullscreen mode Exit fullscreen mode

5. Cherry-Picking Multiple Commits

If you need to cherry-pick a range of commits, specify the range using the .. syntax:

git cherry-pick <start-hash>..<end-hash>

Enter fullscreen mode Exit fullscreen mode

For example:

git cherry-pick abc123..def456

Enter fullscreen mode Exit fullscreen mode

This applies all commits from abc123 (exclusive) todef456 (inclusive).

6. Backporting Changes

If you need to backport a fix from the main branch to older versions, follow the same cherry-picking process. Ensure you're on the target branch before applying the selected commits.

Best Practices When Cherry-Picking

If you need to backport a fix from the main branch to older versions, follow the same cherry-picking process. Ensure you're on the target branch before applying the selected commits.

Best Practices When Cherry-Picking

  1. Minimize Overuse: While cherry-picking is powerful, overusing it can lead to fragmented commit histories. Use it judiciously for isolated changes.

  2. Document Your Changes: Clearly document why a commit was cherry-picked. This helps maintain transparency and avoids confusion in the future.

3. Re-test Thoroughly:
Even though you’re applying a tested change, ensure it’s retested in the target branch to avoid unexpected behavior.

4. Avoid Altering Commit History:
Cherry-picking rewrites commit history. Ensure this doesn't conflict with team workflows or branch policies.

Example Scenario: Resolving a Pull Request Discrepancy

Imagine a scenario where two pull requests (PR1 and PR2) both modify the same file. When PR1 is merged into main, it introduces changes that create a conflict with PR2. Merging PR2 directly is no longer possible.

Steps to Resolve:

  1. Merge PR1 into main.

  2. Switch to the branch for PR2 and rebase it onto main to identify conflicts.

  3. Resolve conflicts manually and cherry-pick relevant commits from PR2 into main if necessary.

  4. Test and verify the changes.

Conclusion
Cherry-picking is an essential tool in every software engineer’s Git toolkit. It empowers developers to manage selective changes effectively, especially in complex scenarios like resolving pull request discrepancies, backporting fixes, and deploying isolated hotfixes. By understanding the use cases and following the outlined steps, you can leverage cherry-picking to maintain clean, functional, and conflict-free codebases.

LinkedIn Account : LinkedIn
Twitter Account: Twitter
Credit: Graphics sourced from Jetbrains

Top comments (0)