What is the difference between git cherry-pick and git rebase?
Understand git cherry-pick vs git rebase — selective commit copying versus replaying a whole branch onto a new base — with clear examples and use cases.
Expected Interview Answer
git cherry-pick copies one or more specific commits onto your current branch, while git rebase moves or replays a whole series of commits onto a new base. Cherry-pick is selective; rebase is wholesale.
Cherry-pick is surgical: you name the exact commits you want and Git applies each as a new commit on your current HEAD, leaving the originals in place. Rebase takes an entire range of commits from your branch and reapplies them on top of another commit, rewriting that range and typically dropping the old line of history. Both create new commit hashes, but cherry-pick duplicates a few chosen commits across branches, whereas rebase relocates and linearizes a branch's whole history.
- Cherry-pick: pull a single fix into another branch without merging everything
- Cherry-pick: backport hotfixes to a release branch
- Rebase: keep a feature branch up to date on the latest main
- Rebase: produce a clean, linear history without merge commits
- Both avoid extra merge commits when used appropriately
AI Mentor Explanation
Cherry-pick is like lifting one brilliant six from last week's match and splicing it into this week's highlights reel, leaving the original innings untouched. Rebase is like re-shooting the entire innings from a different camera angle so the whole sequence sits on a new baseline — you relocate every ball, not just one memorable shot.
Step-by-Step Explanation
Step 1
Identify the goal
Do you need a few specific commits (cherry-pick) or to move a whole branch onto a new base (rebase)?
Step 2
Cherry-pick a commit
Check out the target branch, then git cherry-pick <commit-sha> to apply that commit as a new commit on top.
Step 3
Cherry-pick a range
Use git cherry-pick A^..B to apply an inclusive range of commits in order.
Step 4
Rebase onto a base
From your feature branch, git rebase main replays your branch's commits on top of the latest main.
Step 5
Resolve conflicts
For either command, fix conflicts, git add, then git cherry-pick --continue or git rebase --continue; --abort cancels.
What Interviewer Expects
- Cherry-pick copies chosen commits; rebase moves a whole range
- Both produce new commit hashes
- Cherry-pick leaves original commits in place; rebase rewrites history
- When to backport a fix vs. update a feature branch
- How conflicts and --continue/--abort work in each
Common Mistakes
- Thinking cherry-pick and rebase are interchangeable for any task
- Cherry-picking many commits when a rebase would be cleaner (or vice versa)
- Assuming cherry-pick removes the commit from its source branch
- Rebasing shared/pushed history and breaking teammates' branches
- Cherry-picking a merge commit without -m and getting confused by ambiguity
Best Answer (HR Friendly)
“Cherry-pick copies one or a few specific commits from one branch to another, like grabbing just the fixes you need. Rebase moves an entire branch's set of commits to sit on top of a newer starting point, giving you a clean, straight-line history.”
Code Example
# Cherry-pick: copy one commit onto the current branch
git checkout release
git cherry-pick a1b2c3d # applies that commit as a new commit
# Cherry-pick a range (inclusive of A..B, excluding A itself)
git cherry-pick a1b2c3d^..e4f5g6h
# Rebase: replay the whole feature branch on top of main
git checkout feature
git rebase main # feature commits get new hashes on latest main
# Both use the same conflict flow:
git add .
git rebase --continue # or: git cherry-pick --continue
git rebase --abort # or: git cherry-pick --abortFollow-up Questions
- When would you cherry-pick instead of merge?
- How do you cherry-pick a merge commit?
- What is the risk of rebasing a shared branch?
- How does git rebase --onto let you move a range precisely?
- Does cherry-pick change the original commit on its source branch?
MCQ Practice
1. Which command copies a single specific commit onto your current branch without moving the whole history?
git cherry-pick applies the named commit(s) as new commits on the current branch, leaving originals untouched.
2. What does git rebase main do when run from a feature branch?
Rebase reapplies the feature branch's commits onto the tip of main, creating new commit hashes and a linear history.
3. After cherry-picking commit X from branch A to branch B, commit X on branch A is:
Cherry-pick duplicates the change as a new commit on B; the original commit remains on branch A.
Flash Cards
git cherry-pick in one line — Copies one or more chosen commits onto the current branch as new commits, leaving the originals in place.
git rebase in one line — Replays a whole range of commits onto a new base commit, rewriting that history into a linear sequence.
Key difference — Cherry-pick is selective (specific commits); rebase is wholesale (an entire branch/range moved to a new base).
Do both change hashes? — Yes — both create new commit objects, so cherry-picked and rebased commits get new SHA hashes.