Sub topic 6
Cherry-pick options: -x, -n, ranges, and merges
This chapter assumes you can already run git cherry-pick <commit> to copy one commit onto your current branch, and that you can read git log output. Here you will learn the four options that change how a pick behaves: recording the source commit, skipping the automatic commit, picking several commits at once, and picking a merge commit.
Recording the origin with -x
A plain cherry-pick creates a new commit with a new hash. Nothing in the history links it to the original. Six months later, nobody can tell that the fix on release-1.4 came from main.
The -x option appends a line to the new commit message that names the original commit.
git switch release-1.4
git cherry-pick -x 3f2a9c1The new message ends with a line like this:
(cherry picked from commit 3f2a9c1e5b7d4a8c9e0f1a2b3c4d5e6f7a8b9c0d)Use -x when you pick from a public branch to a maintenance branch, because other people will want to trace the change. Skip it for picks between private branches, where the extra line only adds noise. Git also does not add the line when a pick results in a conflict you later resolve by hand in some workflows, so check the message with git log -1 if the trace matters to you.
Applying without committing with -n
By default, cherry-pick commits right away. The -n (--no-commit) option applies the change to the working tree and index, then stops. You can inspect, edit, or combine it with other work before you commit.
git cherry-pick -n 3f2a9c1
git status
git diff --stagedThis is useful when the original commit needs a small adjustment for the target branch. It also lets you fold several picks into one commit: run -n for each, then make a single git commit.
Picking a range
Give cherry-pick a range with two dots to copy several commits in order.
git cherry-pick A..BThe range A..B excludes A and includes B. So A is the last commit you do not want, and B is the last one you do. If you want A included too, write A^..B.
Git applies the commits oldest first, creating one new commit for each. If one conflicts, the sequence pauses. You resolve it and continue, as the next chapter explains.
Before picking a range, run git log --oneline A..B to see exactly which commits will be copied. This catches off-by-one mistakes with the excluded end.
Picking a merge commit with -m
A merge commit has two or more parents. Cherry-pick copies the change a commit made relative to its parent, so with a merge it cannot know which parent to compare against. Git refuses and reports an error.
The -m option picks the parent, numbered from 1.
git cherry-pick -m 1 <merge-commit>Parent 1 is the branch you were on when you ran git merge, usually the main line. With -m 1, Git computes the diff between the merge and that parent. The result is the whole set of changes the merged branch brought in, applied as a single commit.
Summary of options
| Option | Effect | Typical use |
|---|---|---|
| `-x` | Adds "cherry picked from" line | Backports to release branches |
| `-n` | Applies changes without committing | Adjusting or combining picks |
| `A..B` | Picks a range, excluding `A` | Copying a series of fixes |
| `-m N` | Chooses parent N of a merge | Copying a merged feature |
Exercise
You want to copy commits C2, C3, and C4 from main onto release-1.4. The history on main is C1, C2, C3, C4 (oldest to newest). Write the range command, and say which option you would add so the new commits record where they came from.
Show answer
Run git cherry-pick -x C1..C4 while on release-1.4. The range excludes C1 and includes C2, C3, and C4. The -x option adds a "cherry picked from commit" line to each new message. Equivalent: git cherry-pick -x C2^..C4.
Exercise
Git prints an error when you cherry-pick a merge commit. Why does it happen, and what do you add to fix it?
Show answer
A merge commit has several parents, so Git cannot tell which parent to diff against. Add -m 1 to use the first parent (normally the branch the merge was made on), for example git cherry-pick -m 1 <merge-commit>.