How to copy commits from one branch to another?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Copying commits from one branch to another is a normal Git task when you backport a fix, move a hotfix to a release branch, or recover work that landed on the wrong branch. The usual tool is git cherry-pick, but the right strategy depends on whether you need one commit, a specific range, or an entire branch.
Use cherry-pick for Specific Commits
If you want one commit from another branch, switch to the destination branch and cherry-pick the source commit hash.
That creates a new commit on the current branch with the same patch content as the original commit. The hash changes because the new commit has a different parent.
This is the standard choice for backports because it copies only the fix you asked for.
Copy Several Commits Deliberately
You can cherry-pick more than one explicit commit:
For a contiguous block of history, use a range:
That includes start_hash through end_hash in order. It is useful when a fix spans a short run of clean commits.
Add -x for Traceability
When copying a fix into a maintenance or release branch, -x is often worth using.
Git adds a line to the new commit message showing which original commit it came from. That is helpful later when you need to answer questions such as whether a bug fix was backported from main.
Resolve Conflicts With a Clean Workflow
Cherry-picks can conflict if the destination branch has drifted. The normal conflict workflow is:
If you decide the commit should not be copied after all, abort instead of leaving the repository mid-operation.
Do not mix unrelated edits into a half-resolved cherry-pick. Finish or abort first.
Use Merge When the Whole Branch Should Move
If you actually want all changes from the source branch, merge may be the cleaner tool.
A merge preserves branch history and relationships. Cherry-picking many commits one by one is usually the wrong approach when the real intent is to integrate the whole branch.
Verify the Result
After copying commits, inspect both the history and the patch.
Then run the relevant tests or checks for the changed area.
A clean cherry-pick does not guarantee the code still behaves correctly in the destination branch context.
A Practical Backport Workflow
A disciplined backport process keeps history easier to audit:
- Merge the original fix into the main development branch.
- Switch to the release branch.
- Cherry-pick the fix with
-x. - Resolve conflicts carefully if they appear.
- Run branch-specific tests.
- Review the backport separately if your team uses pull requests.
This approach makes it easier to track which supported branches received the fix.
Common Pitfalls
The biggest mistake is cherry-picking a commit without the prerequisites it depends on. A patch may rely on earlier refactors that are missing from the destination branch.
Another common issue is using cherry-pick when merge would better represent the real intent. Developers also sometimes skip verification because the cherry-pick applied cleanly, which is not the same thing as proving the result works.
Summary
- Use
git cherry-pickwhen you need to copy specific commits selectively. - Use
-xwhen traceability matters, especially for backports. - Cherry-pick ranges carefully when several adjacent commits belong together.
- Use merge instead when the full branch should move as a unit.
- Always verify the destination branch after copying the commits.

