How to merge a specific commit in Git
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When you need one change from another branch without merging everything, the right Git tool is usually cherry-pick. This pattern is common for release backports, hotfixes, and selective patch transfers. A safe workflow includes commit inspection, conflict handling, and traceability.
Confirm the Exact Commit First
Before applying anything, identify and review the commit you want.
Inspect patch content:
Do not skip this review. Many commit messages are broader than the actual code change and may include side effects.
Apply One Commit with Cherry Pick
Switch to target branch and apply selected commit.
For better audit history, include original reference automatically:
-x appends source hash info to commit message, which helps future debugging.
Resolve Conflicts Correctly
If branches diverged, conflicts may occur. Handle them in a controlled sequence.
Resolve files, stage, then continue:
If you need to cancel:
After conflict resolution, run tests in target branch context before pushing.
Pick Multiple Commits Safely
When change spans several commits, you can pick a range or specific list.
Range example:
List example:
Prefer explicit lists when commits are not contiguous or contain unrelated edits.
Use No Commit Mode for Fine Control
Sometimes you want to inspect or edit patch before committing.
This is useful when target branch needs a subset of the original change.
Validate History and Push
After picking, confirm local history and verify build integrity.
Then push branch:
If using pull requests, mention original commit hash and rationale so reviewers understand scope.
When Merge Is Better Than Cherry Pick
Cherry pick is ideal for isolated fixes. Use branch merge when changes are tightly coupled and you need full context.
Choose merge if:
- many dependent commits are required.
- branch is already review approved for full integration.
- preserving branch history is important.
Choose cherry pick if:
- release branch needs one targeted fix.
- full branch includes unfinished work.
- backport scope must stay minimal.
Backport Checklist for Teams
Before applying patches to release branches, use a checklist:
- Review patch content and dependencies.
- Confirm target branch compatibility.
- Run tests after cherry pick.
- Record original commit reference.
- Ensure rollback strategy exists.
Checklists reduce emergency mistakes during production incidents.
Special Case: Cherry Picking Merge Commits
If the source commit is a merge commit, Git needs a mainline parent reference.
Use this only when you understand parent context. In many cases it is safer to cherry pick the underlying non merge commits instead of replaying an entire merge diff.
Common Pitfalls
- Cherry picking without inspecting actual patch content.
- Picking commits with hidden dependencies on earlier changes.
- Forgetting traceability flags or notes.
- Resolving conflicts quickly without rerunning tests.
- Reapplying already picked commits and creating duplicate logic.
Summary
- Use
git cherry-pickfor selective commit transfer. - Inspect source commit before applying to target branch.
- Resolve conflicts carefully and validate behavior with tests.
- Use
-xfor clear provenance. - Prefer full branch merge when change dependencies are broad.
Related reading
- How to merge a transparent png image with another image using PIL
- How to merge master branch into main
- How to merge remote changes at GitHub?
- How to merge specific files from Git branches
- How to merge two BST's efficiently?
- How to merge two sorted arrays in Swift?
- how to merge two sorted integer array in place using On time and O1 space cost
- How to migrate GIT repository from one server to a new one
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.