Copy Git commits to another branch

Cherry-pick selected changes onto a destination branch, preserve attribution, and choose between replaying commits, merging history, and copying a file.

Best practices
Illustration of Git branches and version history

Switch to the destination branch and cherry-pick the commit you want to bring over:

git switch release-1.2
git log feature-x --oneline
git cherry-pick -x a1b2c3d

Use an actual commit ID, save local work first, then review and test the result before pushing the destination branch. A cherry-pick replays the commit's change relative to a parent. It normally creates a new commit on the current branch while leaving the source branch in place.

Copy several changes

# Separate commits, in dependency order:
git cherry-pick a1b2c3d b2c3d4e c3d4e5f
# A linear range including its first commit:
git cherry-pick 'a1b2c3d^..c3d4e5f'
# Combine selected changes into a single new commit:
git cherry-pick -n a1b2c3d b2c3d4e
git diff --cached
git commit -m "Backport selected fixes"

These are alternative workflows. A range is a reachability expression, so merged side histories can add commits beyond a simple sequence. Inspect it with git log --reverse --oneline RANGE before applying it. A root commit has no ^ parent.

Choose the operation by intent

Goal Suitable operation
Bring over selected committed changes Cherry-pick
Integrate a branch and its ancestry Merge
Replay a private branch onto a new base Rebase
Copy one file's snapshot Restore from that branch, then stage and commit
Transfer uncommitted work A reviewed stash or patch workflow

Rebase changes the branch being rebased; it is not simply a copy of a whole branch. A merge can fast-forward or create a merge commit and records ancestry differently from cherry-pick.

Handle conflicts and already-present changes

After resolving a conflict, stage the affected files and run git cherry-pick --continue. Use --abort to abandon the sequence or --skip to omit the current pick. A change already present may produce an empty result rather than duplicate content.

Picking a merge commit requires choosing a mainline parent, such as -m 1; the resulting change is relative to that parent. Prefer individual changes when that better expresses the intended backport.

Attribution and reversal

Cherry-pick normally preserves the recorded author and author date while creating new committer metadata. -x adds a provenance line automatically for conflict-free picks; inspect the message after resolving conflicts if that line is required. The usual result has a different ID, although a permitted fast-forward with --ff can retain an existing commit.

To undo a published pick, revert the destination commit. For an unshared latest commit, reset may be suitable after preserving work; a hard reset discards local tracked changes. Later merges are not guaranteed to conflict merely because equivalent changes exist on both branches.

See git-cherry-pick and git-merge.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting