Bringing One Specific Commit from Another Branch Using git cherry-pick
1. What is it?
If you need only one specific commit from another branch, use:
git cherry-pick <commit-hash>
git cherry-pick takes the changes introduced by a particular commit and applies them to your current branch as a new commit.
This is useful when you do not want to merge the entire branch.
For example:
feature/payment:
A --- B --- C --- D
feature/login:
A --- B --- E
Suppose you need only commit C from feature/payment, not D.
You can cherry-pick C into feature/login.
2. Simple Syntax / Sample
First, find the commit:
git log --oneline feature/payment
Example:
8ab1234 Add payment logging
5cd5678 Fix payment validation
2ef9012 Add payment service
Suppose you need:
5cd5678 Fix payment validation
Switch to the branch that should receive the commit:
git switch feature/login
Then:
git cherry-pick 5cd5678
Finally:
git push origin feature/login
3. Detailed Explanation
Suppose the branches look like this:
C --- D feature/payment
/
A --- B --------
\
E feature/login
You need commit C, but you don't need commit D.
If you merge the whole branch:
git merge feature/payment
you may bring in both C and D.
Instead, switch to your target branch:
git switch feature/login
Then:
git cherry-pick <C-commit-hash>
Git applies the changes from C to feature/login.
The result becomes:
C --- D feature/payment
/
A --- B --------
\
E --- C' feature/login
Notice:
C → original commit
C' → cherry-picked commit
Although the code changes may be the same, C' is generally a new commit with a different commit hash because it has a different parent/history.
4. What Happens Internally?
When you run:
git cherry-pick 5cd5678
Git essentially:
Find commit 5cd5678
↓
Determine changes introduced
by that commit
↓
Apply those changes to
current branch
↓
Create a new commit
It does not merge the complete source branch.
That is the main reason to use cherry-pick.
5. What If There Is a Conflict?
Suppose:
git cherry-pick 5cd5678
produces:
CONFLICT (content): Merge conflict in PaymentService.cs
Check:
git status
Resolve the conflicting code manually.
Then:
git add PaymentService.cs
git cherry-pick --continue
The flow is:
git cherry-pick <commit>
↓
Conflict
↓
Resolve conflict
↓
git add <file>
↓
git cherry-pick --continue
If you want to cancel the operation:
git cherry-pick --abort
This returns you to the state before the cherry-pick started.
6. Cherry-Pick vs Merge
git cherry-pick |
git merge |
|---|---|
| Applies selected commit(s) | Integrates branch histories |
| Good when you need a specific change | Good when you need the branch's work as a whole |
| Creates new commit(s) on target branch | Usually preserves original commits/history |
| Does not merge entire source branch | Integrates all relevant source-branch commits |
| Useful for targeted fixes | Useful for normal branch integration |
Example
Source:
A --- B --- C --- D
Need only C:
git cherry-pick <C>
Result:
Target:
A --- B --- E --- C'
If the intention is to integrate the entire feature branch, merge or rebase is generally more appropriate than cherry-picking each commit individually.
7. Practical Hotfix Example
Cherry-pick is particularly useful when a fix exists on one branch and needs to be applied selectively elsewhere.
For example:
develop
↓
A --- B --- C --- F
↑
Bug fix
Suppose Production's branch needs only F, not all current development changes.
After following the team's release/hotfix process, you could apply that fix with:
git switch hotfix/production
git cherry-pick <F-commit-hash>
Result:
develop:
A --- B --- C --- F
production hotfix:
A --- B --- F'
This allows the fix to move without bringing unrelated development work.
8. Cherry-Picking Multiple Commits
You can also cherry-pick multiple specific commits:
git cherry-pick abc123 def456
Git applies them in the order specified.
For a contiguous range, Git also supports ranges, but for an interview question asking about one specific commit, the key command is simply:
git cherry-pick <commit-hash>
9. Key Points
- Use
git cherry-pickwhen you need specific commit(s) rather than an entire branch. - Find the commit hash using:
git log --oneline
- Switch to the branch that should receive the change:
git switch feature/login
- Apply the commit:
git cherry-pick <commit-hash>
- Cherry-pick creates a new commit on the target branch.
- The new commit normally has a different hash from the original.
- If there is a conflict:
git add <file>
git cherry-pick --continue
- To cancel:
git cherry-pick --abort
- Build and test after cherry-picking.
- Don't use repeated cherry-picks as a substitute for proper branch integration when you actually need the whole branch.
Easy way to remember
Need entire branch
↓
merge
Need one specific commit
↓
cherry-pick
10. Interview Answer
If I need only one specific commit from another branch, I would use
git cherry-pick. First, I would identify the commit hash usinggit log --oneline, switch to the branch where I want the change, and rungit cherry-pick <commit-hash>. Git applies the changes from that commit and creates a new commit on my current branch. If there is a conflict, I would resolve it, stage the resolved files, rungit cherry-pick --continue, and then build and test the application before pushing.