git
version control
patch
branch management
code merging

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.

Browse interview questions

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.

bash
git switch release
git cherry-pick abc1234

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:

  • 'feature contains several commits'
  • one of them is a bug fix you also need in main
  • you do not want the rest of feature

Use:

bash
git switch main
git cherry-pick abc1234

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.

bash
git switch release
git cherry-pick abc1234^..def5678

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:

bash
git show abc1234 > change.patch
git switch release
git apply change.patch

After that, you still need to inspect, stage, and commit the result yourself.

bash
git add .
git commit -m "Apply changes from abc1234"

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.

bash
git cherry-pick -x abc1234

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:

bash
1git cherry-pick abc1234
2# resolve files
3git add path/to/file
4git cherry-pick --continue

If you decide not to proceed:

bash
git cherry-pick --abort

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 apply when the problem is really "copy this commit here," which git cherry-pick already solves.
  • Cherry-picking a commit whose dependencies are missing on the target branch.
  • Forgetting -x when 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-pick in most cases.
  • 'git cherry-pick replays the commit as a new commit on the current branch.'
  • Use git apply only when you specifically want to work from patch text rather than commit history.
  • 'git cherry-pick -x is 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.