Partly cherry-picking a commit with Git
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Sometimes one commit contains two or three unrelated changes, but you only want part of it on another branch. Git does not provide a one-command “partial cherry-pick” that selects individual hunks directly, but you can achieve the same result safely by applying the commit without committing, then staging only the parts you want.
That workflow is practical and common. The important goal is to keep the resulting history traceable and to verify that the selected subset still works on its own.
The Standard Partial Cherry-Pick Workflow
The most reliable pattern is:
- apply the commit without creating a commit
- unstage everything
- stage only the wanted hunks
- commit the selected subset
Commands:
--no-commit is the crucial flag because it applies the patch to your working tree without finalizing history immediately.
When File-Level Selection Is Enough
If you want whole files from the source commit rather than selected hunks, restoring specific files is simpler.
For older Git versions:
This is cleaner than a hunk-based workflow when the desired split happens at the file boundary.
Conflicts and Traceability
Conflicts can still happen during git cherry-pick --no-commit if the target branch differs significantly from the source context.
If that happens, resolve the files first, then continue with the partial-staging workflow.
If the state becomes too messy, abort and start over.
A good commit message should mention the source commit explicitly so reviewers can trace provenance later.
Sometimes the Better Fix Is to Split the Source Commit First
If you control the source branch, a cleaner long-term option is to split the original commit with interactive rebase and then cherry-pick the resulting smaller commit normally.
That looks like this in principle:
Mark the commit for edit, reset it, recommit the logical pieces separately, and then cherry-pick the precise one you need.
This approach rewrites source history, so it is only appropriate when that branch has not been shared in a way that would make rebasing disruptive.
Validate the Result Before Pushing
Partial picks are more fragile than full picks because excluded hunks may have contained hidden dependencies. After the new commit is created, inspect it carefully.
Then run focused tests for the touched code. A partial cherry-pick that compiles but loses setup, migration logic, or a related config change can still fail at runtime.
Common Pitfalls
A common mistake is running plain git cherry-pick <sha> first and only afterward trying to edit the resulting commit. That makes it harder to separate the wanted and unwanted hunks cleanly.
Another issue is forgetting git reset after --no-commit, which leaves everything staged and makes selective staging more awkward.
Developers also sometimes partial-pick commits that were never logically separable in the first place. If the original commit mixed tightly coupled changes, the safest fix may be to split it properly on the source branch.
Finally, do not skip testing. Partial picks create custom history, and custom history deserves extra verification.
Summary
- Git does not have a one-command hunk-level cherry-pick, but
git cherry-pick --no-commitplusgit add -pachieves the same result. - Use file restore instead when selection happens at file boundaries.
- Mention the source commit in the new commit message for traceability.
- Consider splitting the original commit first if you control the source branch.
- Always validate partial picks with diffs and targeted tests.

