Introduction
A practical, real-world guide to understanding and applying Git cherry-pick with actual developer mistakes and fixes.
What is git cherry-pick
?
git cherry-pick
lets you selectively apply commits from one branch to another — like copying specific code changes without merging entire branches.
Now let’s understand a quick comparison cherry-pick
vs merge
vs rebase
Command | What it Does | When to Use |
---|---|---|
git cherry-pick |
Apply specific commits to current branch | You want only certain commits, not the full branch |
git merge |
Combine entire branch history into current branch | You want to bring all commits and preserve history |
git rebase |
Move/Replay commits onto another base branch | You want a clean, linear history, avoid merge commits |
TL;DR:
- Use
cherry-pick
for precision, specific commits only - Use
merge
to combine full branch history - Use
rebase
to clean history and avoid unnecessary merges
Why You Might Need cherry-pick
- You committed on the wrong branch (happens more than we admit)
- You need a bug fix from one branch to another
- You want to copy only certain commits, not merge everything
- You made changes, reverted them locally, but commits got mixed up
My Real-Time Scenario: How I Discovered cherry-pick
Let me share how I personally ran into this:
I made some changes in a branch (let's call it branch A) but accidentally reverted the commits locally, leaving them in the unstaged area. Later, I re-committed those changes — but this time, I was working on branch B, where I only wanted my latest changes, not the leftover ones from branch A.
I pushed the branch to GitHub, went to open the PR, and BOOM — I saw two commits:
- One commit with my intended change for
branch B
- But another unintended commit sneakily came from branch A
Total mess, right? But no worries — I fixed this cleanly using git cherry-pick
.
I removed the wrong commit from branch B
, stayed on branch A
, cherry-picked only the commit I actually needed onto branch B
, and pushed it again. Clean history, problem solved.
Step-by-Step Practical Guide to git cherry-pick
1. View Commit History with git log
git log --oneline --graph --all
✅ This shows a simple, visual commit history across all branches.
Tip: If you're working on a shared repository with your team and didn't create your own branch yet, you can check history of a specific branch like:
git log <branch-name> --oneline --graph --all
Example:
git log main --oneline --graph --all
✅ Use this to find the commit hash (abc123
) you want to cherry-pick.
✅ Copy that hash — you'll need it in the next step.
2. Switch to Your Target Branch
Before you apply the commit, make sure you're on the correct branch:
git status
This shows your current branch.
If unsure, list all branches:
git branch
Switch to your target branch:
git checkout <your-branch-name>
Example:
git checkout feature/login
3. Cherry-Pick the Commit
git cherry-pick <commit-hash>
✅ This applies only the specific commit to your current branch, without merging the entire source branch or bringing unrelated changes.
Example:
git cherry-pick abcd1234
If the commit applies cleanly, you're done. If there's a conflict, Git will pause and you resolve it manually.
Cherry-Pick Multiple Commits
One by one:
git cherry-pick <commit1> <commit2>
Range of commits:
git cherry-pick commitA^..commitB
Handling Conflicts During Cherry-Pick
Conflict? No stress. Git pauses:
CONFLICT (content): Merge conflict in file.js
Resolve it:
git add .
git cherry-pick --continue
Or abort:
git cherry-pick --abort
You can also follow this short youtube video if you want:
Final Thoughts
Mistakes happen — commits go to the wrong place, branches get mixed up.
git cherry-pick
is the clean, targeted way to:
✅ Move specific commits
✅ Fix accidental commits
✅ Avoid messy merges
Once you try it practically, you’ll use it confidently — like I did when I accidentally messed up branches.
Top comments (0)