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 between branches is a normal Git workflow when you need selected fixes in another line of development. The safest method depends on scope: one commit, a commit range, or full branch history. Understanding cherry-pick, merge, and rebase tradeoffs prevents duplicate history, conflict churn, and accidental rewrites.
Choose the Right Strategy
Use this quick rule:
- Copy one or a few specific commits:
cherry-pick. - Bring all changes from source branch:
merge. - Replay branch commits on top of another base:
rebase.
Most “copy commits” tasks are cherry-pick operations.
Method 1: Copy a Single Commit With Cherry-Pick
Checkout target branch first.
This creates a new commit on target branch with same changes but different commit hash.
Verify history:
Method 2: Copy Multiple Commits
Cherry-pick several commits explicitly:
Or copy a contiguous range:
Range syntax is useful for backporting related patches from feature branches.
Handle Cherry-Pick Conflicts
Conflicts are common when branches diverged.
Abort if you decide not to proceed:
Always run tests after conflict resolution because semantic conflicts may remain even when Git merge markers are gone.
Method 3: Merge Entire Source Branch
If you want all commits from source branch, merge is simpler.
Merge preserves branch topology and does not rewrite commit history.
Use when team wants explicit integration commits and full audit trail.
Method 4: Rebase for Linear History
Rebase replays commits from one branch onto another base.
This is not usually described as “copy commit,” but functionally it reapplies commits in new context. Use cautiously on shared branches because history rewrite may require force push.
Verify What You Copied
After copy operations, inspect commit and patch content.
For backports, add traceability with -x option.
This appends source commit hash to commit message, which is useful for audit and maintenance.
Practical Backport Workflow
Typical release backport sequence:
- Checkout release branch.
- Cherry-pick bug fix commits from main.
- Resolve conflicts.
- Run release branch tests.
- Push and open pull request.
Example:
This keeps release changes minimal and traceable.
Avoid Duplicate Patch Confusion
If the same patch is cherry-picked multiple times, history can become noisy. Use git cherry and git log --cherry style checks when unsure whether patch is already applied.
This helps avoid redundant backport commits.
Keeping branch naming and backport labels consistent also makes release audits much easier.
Common Pitfalls
A common pitfall is cherry-picking commits in dependency-wrong order, causing build failures. Another issue is forgetting to checkout the target branch before running cherry-pick. Teams also sometimes cherry-pick merge commits without understanding parent selection, leading to unexpected changes. Rebase on shared branches without coordination is another frequent source of force-push disruption. Finally, copying commits without test validation can propagate partial fixes that pass merge but fail runtime behavior.
Summary
- Use cherry-pick for selective commit copying between branches.
- Use merge when you need all source-branch changes preserved.
- Use rebase for linear history when branch-sharing constraints allow it.
- Resolve conflicts carefully and verify with tests.
- Add traceability with
cherry-pick -xfor maintenance workflows.

