Featured image of post Safe Usecases of git cherry-pick for Specific Commits Featured image of post Safe Usecases of git cherry-pick for Specific Commits

Safe Usecases of git cherry-pick for Specific Commits

Learn how to use git cherry-pick to port specific commit changes to another branch, and how to resolve conflicts safely.

Introduction

In team development, you often encounter situations where you need to deploy a specific bug fix from an ongoing feature branch to your production branch (main) immediately, without releasing the half-finished feature itself.

Merging the entire branch would pull in unfinished code.

This is where the git cherry-pick command comes in handy. This article reviews how to safely apply individual commits to a target branch, handle conflicts during execution, and avoid common history pitfalls.


1. What is git cherry-pick?

git cherry-pick is a command that copies the changes introduced by a specific commit from any branch and applies them as a new commit at the tip of your current branch.

Common Usecases

  • Hotfixing production: Applying a critical patch written in a development branch directly to the stable branch without waiting for the full release cycle.
  • Recovering misallocated commits: Moving a commit that was accidentally made on the wrong branch over to the correct one (then resetting the original branch).

2. Basic Steps to Cherry-Pick

Let’s walk through the command steps to apply a single commit.

Step 1: Identify the Commit Hash

First, check your branch history to find the commit hash (e.g., a1b2c3d) of the change you want to copy.

git log --oneline

Step 2: Checkout the Destination Branch

Switch to the branch where you want to apply the change (e.g., main).

git checkout main

Step 3: Run the Cherry-Pick Command

Execute the command specifying the target commit hash:

git cherry-pick a1b2c3d

Git copies the changes from a1b2c3d and creates a new commit on main with the original commit message.

Cherry-Picking Multiple Commits

You can cherry-pick a series of commits by separating hashes with spaces or specifying ranges:

# Cherry-pick multiple individual commits
git cherry-pick a1b2c3d e5f6g7h

# Cherry-pick a range from Commit A to Commit B (A is excluded, B is included)
git cherry-pick A..B

3. Resolving Conflicts During Cherry-Pick

Because cherry-pick applies patch diffs, it will pause if the code surrounding the changes in the destination branch differs significantly, creating a merge conflict.

How to Resolve Conflicts

  1. Fix the conflict markers: Open the conflicting files in your editor and manually resolve the overlapping code blocks.
  2. Stage the resolved files:
    git add <filename>
    
  3. Continue the cherry-pick:
    git cherry-pick --continue
    

Aborting the Process

If the conflict is too complex or you select the wrong commit, you can abort the cherry-pick and return your working directory to its previous state:

git cherry-pick --abort

4. Key Rules and Best Practices

While git cherry-pick is highly convenient, it can lead to history clutter if overused.

  • Avoid Duplicate Commit Tracking Issues: Cherry-picked commits copy content but receive a unique hash. When you merge the original feature branch back into main later, Git has to resolve two commits with identical changes but different hashes. This can cause redundant conflict markers.
  • Append Origin Commit Reference: When cherry-picking on collaborative branches, use the -x flag. This automatically appends the original commit hash to the new commit message, providing clear traceability.
    git cherry-pick -x a1b2c3d
    
    Output message detail: (cherry picked from commit a1b2c3d...)

Conclusion

git cherry-pick is a powerful tool for hotfixes and correcting minor development mistakes. However, it should be reserved as a rescue mechanism. For regular feature releases, rely on standard merging (git merge) or rebasing (git rebase) to keep your branch history clean.