
Module 6 Lesson 3: Cherry-picking commits
Precision version control. Learn how to 'cherry-pick' a single commit from any branch and apply it to yours—perfect for grabbing a hotfix without the rest of a feature's changes.
Module 6 Lesson 3: Cherry-picking commits
Sometimes you don't want a whole branch. You just want that one commit.
Maybe a coworker wrote a great helper function on their feature-x branch, but that branch is messy and not ready to merge. Or maybe you fixed a bug on an experimental branch and want to move just that fix to main.
In this lesson, we master git cherry-pick.
1. What is Cherry-picking?
Cherry-picking is the act of picking a specific commit from one branch and applying it to your current branch.
- It doesn't move the entire branch.
- it doesn't create a merge commit.
- It creates a new commit on your current branch with the exact same content as the source commit.
graph LR
subgraph "Feature Branch"
F1["Fix Typo (SHA: 7f3d)"]
F2["Half-baked feature"]
end
subgraph "Main Branch"
M1["Stable"]
end
F1 -- "git cherry-pick 7f3d" --> M2["Fix Typo (SHA: 1a2b)"]
M1 --> M2
2. How to Cherry-pick
Step 1: Find the ID
You need the SHA-1 ID of the commit you want. You can find this using git log while looking at the other branch.
Step 2: Switch to Target
Switch to the branch you want to receive the commit (e.g., main).
Step 3: Pick the fruit
git cherry-pick 7f3d9a1
Multiple Cherry-picks
You can pick several commits at once:
git cherry-pick 1a2b3c4 5d6e7f8
3. Handling Conflicts
Just like a merge or rebase, cherry-picking can result in conflicts if the code you are "picking" touches lines that have changed on your current branch.
- Fix the conflict.
git add <file>git cherry-pick --continue(or--abortif you change your mind).
4. Why Use It? (The Hotfix Scenario)
The most common use for cherry-picking is the Hotfix.
- You discover a bug in a feature you're building on
feature-z. - You fix it on
feature-z(Commitabc). - You realize
mainneeds that fix right now, butfeature-zitself is broken. - You switch to
main, cherry-pickabc, and push to production. - You go back to
feature-zand keep working.
Lesson Exercise
Goal: Granular commit movement.
- Create a branch
experiment. - Add a file
helper.txt. Commit it. - Add another commit to
experimentthat makes a mess of a different file. - Switch back to
main. - Find the ID of the
helper.txtcommit. - Cherry-pick that ID into
main. - Verify that
helper.txtis now onmain, but the "messy" changes fromexperimentare not.
Observation: You'll see that cherry-picking gives you the ultimate control over what enters your stable history.
Summary
In this lesson, we established:
- Cherry-picking applies a single specific commit to your current branch.
- It requires the SHA-1 ID of the source commit.
- It is ideal for grabbing isolated fixes or functions without merging a whole branch.
- It results in a new commit with a new ID.
Next Lesson: If rebase is a linear timeline, and cherry-pick is a single fruit, what if you want to rewrite your entire branch history? Welcome to Lesson 4: Interactive rebase.