git apply changes from one commit onto another branch
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you want the changes from one commit on another branch, the usual tool is git cherry-pick, not git apply. git apply works with patch text, while git cherry-pick understands commits and recreates the selected commit's changes on your current branch.
The Simple Answer: Use git cherry-pick
Suppose commit abc1234 exists on some source branch, and you want its changes on release.
That applies the diff introduced by abc1234 onto release and creates a new commit there.
This is the normal answer because it preserves the idea of "apply that commit here" without manually handling patch files.
What cherry-pick Actually Does
git cherry-pick does not move the original commit object between branches. Instead, it replays the change as a new commit on the current branch.
That means:
- the content change is preserved
- the new commit has a different hash
- the branch history stays explicit
This is usually what you want when backporting a bug fix or copying one targeted change without merging an entire branch.
Example Workflow
Imagine this situation:
- '
featurecontains several commits' - one of them is a bug fix you also need in
main - you do not want the rest of
feature
Use:
If the source change depends on earlier commits, cherry-picking just one commit may conflict or compile incorrectly. In that case, cherry-pick the dependent commits too, or choose a different integration strategy.
Cherry-Pick A Range Of Commits
You can cherry-pick more than one commit if they are sequential.
That replays the inclusive range from abc1234 through def5678.
This is useful when a fix was split into several small commits and you want to preserve that breakdown on the target branch.
When git apply Makes Sense
git apply is appropriate when you already have patch text and want to apply it to the working tree, usually without importing commit metadata.
For example:
After that, you still need to inspect, stage, and commit the result yourself.
So yes, you can use git apply indirectly, but if the source of truth is already a Git commit, cherry-pick is almost always the cleaner tool.
Keep Commit Metadata Or Not
This is one of the main differences between the two approaches.
git cherry-pick can preserve authorship and optionally record where the change came from.
The -x option appends a line to the new commit message showing the original commit hash. That is useful for backports and audit trails.
git apply does not do that because it works at patch level, not commit level.
Conflict Handling
Both methods can encounter conflicts if the target branch has diverged. cherry-pick gives a more structured conflict workflow because Git knows you are replaying a commit.
The typical conflict flow is:
If you decide not to proceed:
With git apply, you lose some of that higher-level commit-oriented workflow.
When Not To Cherry-Pick
Cherry-pick is great for isolated fixes, but it can become messy if you use it to shuttle large feature sets repeatedly across long-lived branches. In those cases, merge or rebase may be more coherent.
Use cherry-pick when:
- you need one bug fix on another branch
- you are backporting a specific change
- you want precision more than branch integration
Do not use it as a substitute for regular branch strategy.
Common Pitfalls
- Reaching for
git applywhen the problem is really "copy this commit here," whichgit cherry-pickalready solves. - Cherry-picking a commit whose dependencies are missing on the target branch.
- Forgetting
-xwhen traceability matters for backports. - Applying a patch to the working tree and then forgetting to commit it.
- Using cherry-pick repeatedly on large feature branches and creating history that becomes hard to reason about.
Summary
- To apply changes from one commit onto another branch, use
git cherry-pickin most cases. - '
git cherry-pickreplays the commit as a new commit on the current branch.' - Use
git applyonly when you specifically want to work from patch text rather than commit history. - '
git cherry-pick -xis useful when you want traceability back to the original commit.' - For isolated fixes, cherry-pick is the right tool; for broad branch integration, consider merge or rebase.
Related reading
- git apply fails with patch does not apply error
- git archive fatal Operation not supported by protocol
- Git as mercurial client? Why no git-hg?
- Git asks for username every time I push
- Git asks for username every time I push
- Git Bash is extremely slow on Windows 7 x64
- Git Bash won't run my python files?
- Git branch command behaves like 'less
.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.