Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Git Cherry-Pick: Commits, Ranges, and Conflict Handling

A..B excludes A, -x records where the change came from, and one change with two hashes breaks later reverts. Backports need a policy, not a habit.

~5 min readGit
#Git#Cherry-pick#Release#CLI
3D illustration of a commit block lifted by a claw from one branch track onto another

Quick Summary

git cherry-pick copies the change introduced by a commit onto the current branch as a new commit with a new hash. It is the tool for taking one fix from main into a release branch without dragging the rest of main along, and for recovering work that went to the wrong branch.

Ranges follow the usual two-dot rule: A..B means "commits reachable from B but not from A", which excludes A itself. A^..B therefore includes A — the off-by-one that bites everyone the first time.

A cherry-pick runs the same three-way merge as any other merge, so it conflicts the same way, but --ours and --theirs are relative to the copy target, and -x is what preserves any trace of where the change came from. Without -x, identical changes look like independent work, which later makes reverts fail and duplicates hard to trace.

Basic Syntax and Core Signatures

bash
git cherry-pick 3f2a1b9 # one commit onto the current branch git cherry-pick 3f2a1b9 9c1e2f4 # several, applied in the order given git cherry-pick A..B # commits after A up to and including B git cherry-pick A^..B # include A git cherry-pick main~3..main # last three commits of main git cherry-pick -x 3f2a1b9 # append "(cherry picked from commit ...)" git cherry-pick -n 3f2a1b9 9c1e2f4 # stage changes, no commit yet git cherry-pick -e 3f2a1b9 # edit the message before committing git cherry-pick -m 1 9c1e2f4 # pick a merge commit, mainline = parent 1 git cherry-pick --no-commit 3f2a1b9 # same as -n git cherry-pick --abort # return to the pre-sequence state git cherry-pick --continue # after resolving git cherry-pick --skip # drop this one, continue with the rest
bash
# Which commits from a branch are missing upstream, and vice versa git cherry -v main feature/login git log --oneline main..feature/login git log --oneline feature/login..main

git cherry compares by patch-id, not by hash, so it identifies a commit that was already applied with a different hash — which is exactly the situation created by rebasing and cherry-picking.

Key Comparison Table

CommandSource historyTarget historyNew hashTrace of origin
cherry-pick <sha>UnchangedNew commitYesOnly with -x
cherry-pick -xUnchangedNew commitYesMessage cites the source sha
merge <branch>UnchangedMerge commitn/aParent link
rebase <branch>RewrittenRewritten rangeYesNo, but ancestry preserved
squash mergeUnchangedOne commitYesPR reference only
patch / git amn/aNew commitYesAuthor preserved, committer changed
SituationCommandWhy
One fix from main into a release branchcherry-pick -x <sha>Minimal diff, documented origin
Recover a commit made on the wrong branchcherry-pick <sha> then reset the wrong branchSame content, clean target
Several commits as onecherry-pick -n A B C then commitSingle reviewable commit
Commit already applied-n then check git diff --cached is empty; or --skipAvoid an empty commit
Merge commit neededcherry-pick -m 1 <merge>Parent 1 is the mainline

Practical Edge Cases and Deep Dives

1. The range that picks too many commits

A..B on a history containing merges walks everything reachable from B and not from A, including commits from side branches merged in between. On a branch with integration merges, a "last three commits" range can pick up thirty. Use --first-parent to walk only the mainline before picking:

bash
git log --oneline --first-parent main~5..main # inspect first git cherry-pick --first-parent main~3..main # pick only mainline commits

--first-parent for cherry-pick exists since Git 2.34. On older versions, list the hashes explicitly after inspecting the log.

2. Conflicts where the labels are inverted

A cherry-pick applies the source commit's diff onto the current branch, so during a conflict --ours is the branch you are on and --theirs is the incoming change — the same as a merge, unlike a rebase. The reliable way to reason: git log --merge -p <file> lists only the commits involved in the conflict, so you can see which side introduced which line.

3. Empty cherry-picks and --skip

Picking a commit whose change already exists produces an empty commit, and Git stops with "The previous cherry-pick is now empty". Two valid responses: git cherry-pick --skip (drop it) or git commit --allow-empty (keep a marker). Skipping is right for a duplicate; allowing an empty commit deliberately is right when a release tag should show that a fix was accounted for. git cherry-pick --empty=drop makes the choice a flag instead of an interactive decision.

4. Picking a merge commit

A merge has two parents, so its diff is ambiguous; -m 1 says "compute the diff against parent 1". Picking a merge is almost always a mistake — it copies the merge's combined change without the individual commits, and the result cannot be re-merged cleanly. Prefer picking the individual commits, or git rebase --onto.

Duplicated changes. Once a change exists twice with different hashes, git revert of one does not undo the other, and a later merge of the original branch reintroduces the change or conflicts oddly. That is what -x documents. For a long-lived release branch receiving many backports, the sustainable pattern is a label (backport/release-2.4) on the original PR and a script that picks every labelled commit — not manual picks with no record.

5. Scripted backports

For a batch, -n plus one commit keeps review manageable, and a loop keeps the order deterministic:

bash
git switch release/2.4 for sha in $(git log --format=%h --reverse main~5..main -- src/); do git cherry-pick -x "$sha" || { echo "conflict at $sha"; break; } done

Stop at the first conflict rather than continuing; resolving midway through a sequence leaves the branch in a state nobody wants to review.

Backports as policy. A release branch that only receives cherry-picks drifts from main in a way that is invisible until the next release, because the same change exists as two unrelated commits with two different hashes. The symptoms appear late: a fix that is present in both branches behaves differently after a refactor, a revert on main does not affect the release branch, and a merge from the release branch back into main reintroduces old code. Two habits keep this manageable.

Record each backport where the team looks: a backported-to-release-2.4 label on the original pull request, and the -x line in the copied commit. Then, before a release, compare the branches by patch content rather than by log shape:

bash
git log --oneline main...release/2.4 # commits unique to each side git cherry -v main release/2.4 # missing by patch-id, not by hash git diff release/2.4 main -- src/ # real content drift in one directory

When the drift becomes structural, stop picking and merge: create a dedicated branch, resolve the differences once, and merge it into both sides. Manual picks are for emergency fixes measured in days, not for a policy that runs for a year.

One last signal worth watching: if git cherry lists the same change as missing on both sides, the patch-id differs because the file context changed, not because the change is absent. Reading the two diffs side by side is faster than picking again and resolving a conflict that is not real.

Common Git Operation Mistakes

SymptomCauseFix
The first commit of the range is missingA..B excludes AUse A^..B
Dozens of unexpected commits pickedRange crossed a merge--first-parent, or list hashes
"cherry-pick is now empty"Change already applied--skip, or --empty=drop
Revert later fails with "nothing to revert"Change was duplicated by a pickRevert the actual commit that is in this branch
Wrong side kept during a conflictLabels misreadgit log --merge -p <file>, then re-apply
Merge commit picked without -m-m omittedAbort, retry with -m 1
Sequence stops halfwayOne commit conflictedResolve, --continue; --skip only if the change is redundant
Origin of a backport untraceable-x not usedAdd the sha in the message manually, use -x next time

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.