December 5, 2026

git cherry-pick Explained: Copying One Commit, the Range Syntax That Skips One, and What Happens at Merge Time

Metal tongs lifting a single cherry from a cluster on a branch

A fix lands on a feature branch, and production needs it today, without the half-finished work around it. git cherry-pick copies that one commit onto another branch. It does not move the commit; it makes a new one with a new hash, which has consequences later. And its range syntax has a trap: A..B does not include A. Each behavior below was reproduced in a scratch repository with Git 2.43.0, and the output shown is what Git printed.

Video: The Modern Coder on YouTube

What cherry-pick does

A new commit with the same change

The Git documentation describes the command in one sentence: given one or more existing commits, apply the change each one introduces, recording a new commit for each. It also requires a clean working tree, meaning no modifications from the HEAD commit.

The test repository had a feature branch with three commits after base: “add d”, “add e” and “hotfix: add fix.txt”. From main, cherry-picking the hotfix by its hash produced:

[main fd9d70f] hotfix: add fix.txt

A different hash, every time

The original commit on feature was cf40f2c. The copy on main was fd9d70f. Same change, same message, same author, different commit. A commit’s identity includes its parent, and the copy has a different parent, so it cannot have the same hash.

That is the whole mental model. Cherry-pick replays a diff; it creates history rather than sharing it.

Wooden rubber stamp resting on an open book

Recording where a pick came from

-x

Because the hashes differ, nothing in Git links the copy to the original. The -x option adds that link in the message. The documentation says it appends a line reading (cherry picked from commit …) to the original message, so a reader can see where the change came from. In the test, the picked commit’s message ended with:

(cherry picked from commit cf40f2c139bcbafe4012969f8c6e396f110596e4)

The documentation adds two caveats. The line is added only for picks without conflicts. And it advises against using -x when cherry-picking from a private branch, because the referenced commit means nothing to anyone who cannot see it. Between two public branches, such as a release branch and main, the line is the cheapest audit trail available.

Picking a range

A..B leaves A out

Several commits can be picked at once with range syntax, and the range follows Git’s normal rules, which surprise people here. git rev-list shows exactly which commits a range means:

$ git rev-list --oneline feature~2..feature~1
06cc678 add e

$ git rev-list --reverse --oneline feature~2^..feature~1
ddb838f add d
06cc678 add e

The intent was to pick both “add d” (feature~2) and “add e” (feature~1). The first range picked only “add e”, because A..B means commits reachable from B but not from A, and A is reachable from itself. In the first round of testing, that omission caused a conflict: “add e” depended on a line that “add d” had added, and without it Git could not apply the change.

The fix is the caret. feature~2^..feature~1 starts one commit earlier, so the range includes both. Picked that way, the file ended up as a b c d e, in order, with two new commits on main.

The documentation’s own examples

The Git documentation’s examples use the same set notation. git cherry-pick ..master applies the changes introduced by all commits that are ancestors of master but not of HEAD. Running git rev-list --oneline on any range before picking it costs nothing and removes the guesswork.

Railroad tracks

Conflicts

Same rules as a merge

A cherry-pick can conflict like any merge. A release branch that had added its own line X at the end of the file tried to pick “add d”, which added d in the same spot:

CONFLICT (content): Merge conflict in f.txt
error: could not apply ddb838f... add d

git status --short showed UU f.txt, both sides modified. The conflict markers in the file put the release branch’s X against the pick’s d.

Continue, skip or abort

Resolving works the same as in a merge: edit the file, git add it, then git cherry-pick --continue. The test resolved by keeping both lines, and the result was a new commit with the file reading a b c X d.

git cherry-pick --skip drops the current commit and moves on to the next in a range. --abort, which the documentation describes as canceling the operation and returning to the pre-sequence state, put the branch back exactly where it had been: git status was clean and the log ended at the release branch’s own commit, as if nothing had happened.

Merge commits and empty picks

A merge needs a mainline

A merge commit has two parents, so “the change it introduced” depends on which parent it is compared with. Asked to cherry-pick one, Git refused:

error: commit 6769342... is a merge but no -m option was given.
fatal: cherry-pick failed

The documentation gives the reason: usually a merge cannot be cherry-picked because you do not know which side of the merge should be considered the mainline. The -m option names the parent, counting from 1, to replay the change against. With -m 1, meaning “relative to the branch that was merged into”, the test produced one new commit on the target branch that brought in both files the merged topic branch had added, t1.txt and t2.txt.

That is a blunt tool. The new commit flattens a whole branch into one change with no record of the individual commits, and the branches involved now share content without sharing history. Picking the topic’s own commits as a range is usually cleaner.

When the change is already there

Picking a commit whose change already exists on the target branch leaves nothing to commit. In the test, Git stopped with “The previous cherry-pick is now empty”, and suggested either committing it anyway or skipping it; git cherry-pick --skip moved on. The documentation explains that by default, cherry-picking an empty commit fails, so that keeping it requires an explicit choice. In a long range where some fixes were already backported by hand, those stops are expected, and --skip is the right answer to each.

Aerial view of a winding river

Applying without committing

-n

Sometimes the goal is to borrow a change and adjust it before committing, or to combine several picks into one commit. The documentation explains that the command usually creates a sequence of commits, and that -n (--no-commit) instead applies the changes necessary to cherry-pick each named commit to the working tree and index without making any commit.

In the test, git cherry-pick -n on the hotfix left fix.txt staged and nothing committed:

A  fix.txt

From there the change can be edited, tested and committed with a new message, or thrown away with git reset --hard. The same idea, setting work aside without committing, is what the earlier post on git stash covers from the other direction.

What happens when the branches meet

The duplicate in history

The cherry-picked hotfix and the original are two different commits. When feature is eventually merged into main, both arrive. In the test, after cherry-picking the hotfix onto main and then merging feature, the merge succeeded with no conflict, and git log --oneline | grep -c hotfix returned 2: one copy from the pick and one original from the branch.

Git handled it because both commits made exactly the same change to fix.txt, so the merge saw nothing to reconcile. If either side had edited the file further in between, the merge would have had to reconcile two different histories of the same lines, and conflicts become likely. That is the real cost of cherry-picking between branches that will later merge: the history records the change twice, and the merge has to agree with itself.

git cherry

Git can tell which commits have already been copied, by comparing changes rather than hashes. The git cherry documentation says its equivalence test is based on the diff, after removing whitespace and line numbers, which is what lets it detect commits copied by cherry-pick. git cherry -v main feature listed the commits on feature and marked each one:

+ b28624c add d
+ 04e9b3e add e
- cf40f2c hotfix: add fix.txt

A - means an equivalent change already exists on main; a + means it does not. Before cherry-picking a batch of fixes from one long-lived branch to another, that listing shows what is still missing.

A short routine before picking

The tests suggest a habit that prevents most surprises. Run git rev-list --oneline on the range first, so the list of commits is exactly the intended one, including the first. Run git cherry -v against the target branch, so changes already present are known before Git stops on them. Use -x when both branches are public, so the copy points back to its source. And when a pick conflicts in a way that is not obvious, --abort costs nothing: the branch returns to where it was, and the pick can be retried with more care, or replaced by a merge.

When to reach for it

Cherry-pick fits backporting a fix to a release branch, rescuing a single good commit from an abandoned branch, or moving a commit made on the wrong branch. For bringing a whole branch’s work across, a merge or rebase keeps one copy of each change instead of two. Working on two branches side by side without switching at all is a different problem, which the earlier post on git worktree addresses.

Sources

  • git-cherry-pick documentation (git-scm.com): https://git-scm.com/docs/git-cherry-pick
  • git-cherry documentation (git-scm.com): https://git-scm.com/docs/git-cherry

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *