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.

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
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# 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..maingit 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
| Command | Source history | Target history | New hash | Trace of origin |
|---|---|---|---|---|
cherry-pick <sha> | Unchanged | New commit | Yes | Only with -x |
cherry-pick -x | Unchanged | New commit | Yes | Message cites the source sha |
merge <branch> | Unchanged | Merge commit | n/a | Parent link |
rebase <branch> | Rewritten | Rewritten range | Yes | No, but ancestry preserved |
squash merge | Unchanged | One commit | Yes | PR reference only |
patch / git am | n/a | New commit | Yes | Author preserved, committer changed |
| Situation | Command | Why |
|---|---|---|
| One fix from main into a release branch | cherry-pick -x <sha> | Minimal diff, documented origin |
| Recover a commit made on the wrong branch | cherry-pick <sha> then reset the wrong branch | Same content, clean target |
| Several commits as one | cherry-pick -n A B C then commit | Single reviewable commit |
| Commit already applied | -n then check git diff --cached is empty; or --skip | Avoid an empty commit |
| Merge commit needed | cherry-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:
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:
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; }
doneStop 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:
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 directoryWhen 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
| Symptom | Cause | Fix |
|---|---|---|
| The first commit of the range is missing | A..B excludes A | Use A^..B |
| Dozens of unexpected commits picked | Range 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 pick | Revert the actual commit that is in this branch |
| Wrong side kept during a conflict | Labels misread | git log --merge -p <file>, then re-apply |
Merge commit picked without -m | -m omitted | Abort, retry with -m 1 |
| Sequence stops halfway | One commit conflicted | Resolve, --continue; --skip only if the change is redundant |
| Origin of a backport untraceable | -x not used | Add the sha in the message manually, use -x next time |
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit config: Aliases, rerere, and Credential Helpers
Five config scopes decide one value at a time. Aliases are user-level shorthand, rerere records conflict resolutions, and credential helpers choose where your secret lives.
Git Hooks: pre-commit, commit-msg, and Husky Automation
Hooks live in .git/hooks, which a clone never receives, so core.hooksPath and Husky are how they travel. Exit codes abort the operation, and every hook is bypassable.
Git bisect: Binary Search, Automated Scripts, and Bug Hunting
Mark one bad and one good commit and Git halves the range each step. bisect run automates it through exit codes, and monotonicity is the assumption that must hold.
Git ignore: Patterns, Negation, and Untracking Cached Files
Patterns are relative and last match wins, but a negation cannot revive a file inside an excluded directory. Ignore rules never touch tracked files: rm --cached first.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.