Git
Branch Management
Software Development
Commit Copying
Coding Tips

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.

bash
git checkout release/1.4
git cherry-pick abc1234

This creates a new commit on target branch with same changes but different commit hash.

Verify history:

bash
git log --oneline -10

Method 2: Copy Multiple Commits

Cherry-pick several commits explicitly:

bash
git checkout release/1.4
git cherry-pick abc1234 def5678 9ac0f11

Or copy a contiguous range:

bash
git checkout release/1.4
git cherry-pick start_sha^..end_sha

Range syntax is useful for backporting related patches from feature branches.

Handle Cherry-Pick Conflicts

Conflicts are common when branches diverged.

bash
1git cherry-pick abc1234
2# resolve conflicts in files
3
4git add <resolved-files>
5git cherry-pick --continue

Abort if you decide not to proceed:

bash
git cherry-pick --abort

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.

bash
git checkout main
git merge feature/new-reporting

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.

bash
git checkout feature/new-reporting
git rebase main

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.

bash
git show --stat HEAD
git diff target_base..HEAD

For backports, add traceability with -x option.

bash
git cherry-pick -x abc1234

This appends source commit hash to commit message, which is useful for audit and maintenance.

Practical Backport Workflow

Typical release backport sequence:

  1. Checkout release branch.
  2. Cherry-pick bug fix commits from main.
  3. Resolve conflicts.
  4. Run release branch tests.
  5. Push and open pull request.

Example:

bash
git checkout release/1.4
git cherry-pick -x abc1234 def5678
git push origin release/1.4

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.

bash
git cherry release/1.4 main

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 -x for maintenance workflows.

Course illustration
Course illustration

All Rights Reserved.