Skip to main content

Cherry Picking Commits

In the standard Git workflow, we usually combine branches using Merge or Rebase. However, sometimes you don't want the whole branchโ€”you just want one specific "Win."

Cherry Picking allows you to pick a single commit from any branch in your repository and apply it to your current branch.

A Note on Terminology

The term "Cherry Picking" is a bit whimsical, but it perfectly captures the idea of selecting just one "cherry" (commit) from a "bunch" (branch) without taking the whole thing.

The "Grocery Store" Analogyโ€‹

Imagine you are at a grocery store.

  • Merging is like buying the entire pre-packaged "Fruit Basket." You get the apples, the grapes, and the bananas, even if you only wanted the grapes.
  • Cherry Picking is like walking up to the fruit stand, picking one perfect apple, and putting it in your own basket. You leave the rest of the fruit behind.

When to use Cherry Picking?โ€‹

At CodeHarborHub, we use cherry-picking in these specific "Industrial" scenarios:

  1. Hotfixes: You fixed a critical bug in a testing branch, and you need that exact fix in the main branch now, without pulling in all the other unfinished test code.
  2. Undo a Revert: If you accidentally reverted a commit, you can cherry-pick the original commit back into your history.
  3. Collaborative "Theft": Your teammate wrote a perfect Button.js component on their messy experimental-ui branch. You can cherry-pick just that component commit into your clean branch.

Step-by-Step: How to Cherry Pickโ€‹

1. Find the Commit IDโ€‹

First, you need the "ID" (the SHA hash) of the commit you want to steal. You can find this on GitHub or by using:

git log --oneline --all

Look for the 7-character code (like a1b2c3d) next to the commit message.

2. Switch to the Destinationโ€‹

Move to the branch where you want the commit to land:

git checkout main

3. Pick the Cherryโ€‹

Run the command followed by the commit ID:

git cherry-pick a1b2c3d

Merge vs. Cherry Pickโ€‹

FeatureGit MergeGit Cherry Pick
ScopeCombines the entire branch history.Copies one specific commit.
HistoryCreates a "Merge Commit."Creates a brand new commit on your branch.
Use CaseFinishing a feature.Porting a specific fix or component.

Handling Cherry-Pick Conflictsโ€‹

Just like a merge, if the "Cherry" you are picking changes the same lines as your current branch, a conflict will occur.

The Fix:

  1. Open the file in VS Code.

  2. Resolve the conflict as usual.

  3. Run:

    git add .
    git cherry-pick --continue
A Note on Duplicate History

When you cherry-pick, you are creating a copy of a commit. If you later merge the original branch into your current branch, Git is usually smart enough to know they are the same, but it can sometimes lead to a "messy" history. Use cherry-picking sparingly!