Cherry-pick copies a commit from one branch onto another. It is the standard way to backport a fix to a release branch without merging all the unfinished work from main.

git checkout release/1.2
git cherry-pick <sha>

Git applies the change introduced by that commit and creates a new commit with a new ID. The original commit stays where it was. The content is copied; the history is not.

When it is the right tool

  • Backporting a bug fix to a maintenance branch.
  • Moving a commit that was made on the wrong branch.
  • Pulling one change out of a feature branch that is not ready to merge.
  • Recovering a commit from a deleted branch (after finding it in the reflog).

It is the wrong tool for merging a whole feature. If you need most of a branch, merge or rebase; cherry-picking many commits creates duplicates and conflict debt.

Ranges and multiple commits

git cherry-pick A..B        # commits after A up to B, excluding A
git cherry-pick A^..B       # including A
git cherry-pick A B C       # specific commits

The A..B syntax excludes A. If you want A included, use A^..B. This off-by-one is a common source of missing changes.

Conflicts

A cherry-pick applies a diff, so it can conflict even when the original commit applied cleanly. Resolve like a merge:

git status                  # see conflicted files
# edit the files
git add <file>
git cherry-pick --continue

To skip the current commit:

git cherry-pick --skip

To abandon the whole operation:

git cherry-pick --abort

If the conflict is substantial, consider whether the commit depended on other changes that are not present on the target branch. Cherry-picking a commit that assumes a refactor will produce a fix that does not compile.

Duplicate commits

After a cherry-pick, the same change exists as two different commits on two branches. When the branches later merge, Git usually recognizes the patch as already applied and does not duplicate the file change, but the history still shows two commits. This is expected; it is also why long-lived cherry-pick workflows accumulate noise.

A common release process is:

  1. Fix on main.
  2. Cherry-pick to release/1.2.
  3. Cherry-pick to release/1.1 if still supported.
  4. Never merge the release branches back into main.

The fix exists once in content and multiple times in history. That is acceptable for backports, as long as everyone knows the direction of flow.

Empty commits and -x

If the change is already present on the target branch, the cherry-pick may result in an empty commit. Git stops and asks what to do; --skip discards it. --allow-empty keeps it, which is rarely what you want.

The -x flag appends a line to the commit message:

git cherry-pick -x <sha>

It records the source commit ID, which helps audits and future merges. Many projects require it for backports.

Cherry-pick versus rebase

  • Cherry-pick copies selected commits onto the current branch.
  • git rebase --onto replays a range onto a new base, usually to move a whole branch.
  • Merge integrates the entire branch.

If you find yourself cherry-picking ten commits, stop and ask whether a rebase or merge is the honest operation.

A caution about merge commits

Cherry-picking a merge commit requires a mainline:

git cherry-pick -m 1 <merge-sha>

-m 1 means “treat parent 1 as the mainline”. Choosing the wrong parent inverts the change. If the merge is complex, cherry-pick the individual commits instead.

Cherry-pick is a precision tool. It is excellent for one fix, dangerous as a substitute for branch management. The cherry-pick reference includes the conflict workflow and the -x convention.