Git Cherry-Pick
Cherry-picking in Git means copying one or more specific commits from one branch onto another. The command git cherry-pick <commit> takes the change that a commit made, applies it to the current branch, and records it as a new commit. It is used when you need some of a branch's work, not all of it: for example, to bring a bug fix from main to an older release branch, or to rescue a commit that was made on the wrong branch.
The name comes from picking cherries: you take the ripe ones you want from the tree and leave the rest. A merge would take the whole branch. Cherry-pick takes exactly the commits you name.
Syntax
git cherry-pick [options] <commit>...| Form or option | Description |
|---|---|
git cherry-pick <commit> | It is used to copy one commit onto the current branch. |
git cherry-pick <c1> <c2> | It is used to copy several commits, in the order given. |
git cherry-pick <a>..<b> | It is used to copy the commits after <a> up to and including <b>. |
-x | It is used to add "(cherry picked from commit ...)" to the message. |
-n (--no-commit) | It is used to apply the changes without committing, so several can be combined. |
-e (--edit) | It is used to edit the message before committing. |
--continue / --skip / --abort | It is used to go on after a conflict, skip the current commit, or cancel. |
-m 1 | It is used to cherry-pick a merge commit, relative to its first parent. |
Features of Cherry-Pick
- It copies, it doesn't move. The original commit stays on its branch, and the copy gets a new ID.
- It copies the change, not the snapshot. Git applies the difference between the commit and its parent, so the rest of the target branch stays as it is.
- It keeps the author and message. The copy has the original author and author date, and you as the committer.
- It can conflict. If the target branch differs where the change applies, you resolve it as in a merge.
The Starting Point
The school runs version 1.0 of the Fee Tracker, from the branch release/1.0. Development continues on main, which has new features and two bug fixes:
mkdir fee-tracker
cd fee-tracker
git init --quiet
printf 'Term 1 fee: $12,000\nLate fee: $25\n' > fees.txt
printf "Fee Tracker\n\nTracks school fees.\n" > README.md
git add .
git commit --quiet -m "Add the fees"
git branch release/1.0
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipts"
sed -i.bak 's/Late fee: \$25$/Late fee: $250/' fees.txt
rm fees.txt.bak
git commit --quiet -am "Fix the late fee amount"
echo "Receipts are printed after payment." >> README.md
git commit --quiet -am "Describe receipts"
echo "Fees are per term." >> fees.txt
git commit --quiet -am "Say that fees are per term"
git log --oneline --graph --all* 9cba098 (HEAD -> main) Say that fees are per term
* 5871651 Describe receipts
* 51d0c76 Fix the late fee amount
* 65dd060 Add receipts
* 88b2855 (release/1.0) Add the feesThe school needs the late fee fix in 1.0, but not the unfinished receipts feature. Merging main into release/1.0 would bring everything. Cherry-pick brings just the fix.
Example 1: Copy One Commit with -x
git switch --quiet release/1.0
git cherry-pick -x ":/Fix the late fee"[release/1.0 7b898e7] Fix the late fee amount
Date: Mon Jan 5 10:14:00 2026 -0600
1 file changed, 1 insertion(+), 1 deletion(-)cat fees.txt
git log -1 --format=%BTerm 1 fee: $12,000
Late fee: $250
Fix the late fee amount
(cherry picked from commit 51d0c7659648f0f57fd4caecd1653c818740e06f)Explanation: Git applied the change of "Fix the late fee amount" to release/1.0 and committed it with the same message. The commit has a new ID, because its parent is different. -x added the last line, (cherry picked from commit ...), with the full ID of the original, so anyone can see where the fix came from. Use -x whenever you copy between long-lived branches such as release branches.
":/Fix the late fee" names the commit by its message. Usually you would copy the ID from git log --oneline main.
Example 2: Copy Several Commits
The school also wants the "per term" wording. A cherry-pick can take several commits at once, in the order you give them:
git cherry-pick ":/Say that fees are per term"
git log --oneline[release/1.0 d0b4ad9] Say that fees are per term
Date: Mon Jan 5 10:18:00 2026 -0600
1 file changed, 1 insertion(+)
d0b4ad9 (HEAD -> release/1.0) Say that fees are per term
7b898e7 Fix the late fee amount
88b2855 Add the feesFor a range of consecutive commits, git cherry-pick A..B copies the commits after A up to and including B, oldest first. To include A itself, write A^..B.
Example 3: A Conflict
A conflict happens when the target branch has different content where the change applies. Navika changes the README title on the release branch. On main, the title is changed differently, and she tries to cherry-pick that commit:
sed -i.bak "s/^Fee Tracker$/Fee Tracker 1.0/" README.md
rm README.md.bak
git commit --quiet -am "Name the release in the README"
git switch --quiet main
sed -i.bak "s/^Fee Tracker$/Fee Tracker (development)/" README.md
rm README.md.bak
git commit --quiet -am "Mark the README as development"
git switch --quiet release/1.0
git cherry-pick ":/Mark the README as development"Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
error: could not apply ccf8fdf... Mark the README as development
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"The cherry-pick stopped with the same markers as a merge conflict. Navika keeps the release title, so the release branch needs nothing from this commit. She has three choices:
| Command | Description |
|---|---|
Fix the file, git add, then git cherry-pick --continue | It is used to finish the copy with your resolution. |
git cherry-pick --skip | It is used to leave out this commit and go on with the next one, if several were given. |
git cherry-pick --abort | It is used to cancel and return to the state before the cherry-pick. |
git cherry-pick --abort
git status -s
git log --oneline -1b1b9639 (HEAD -> release/1.0) Name the release in the READMEExplanation: --abort restored the branch exactly as it was. The release keeps its own README.
Example 4: Combine Commits with --no-commit
-n applies the changes without committing, so several commits can arrive as one commit:
git cherry-pick -n ":/Add receipts" ":/Describe receipts"
git status -sAuto-merging README.md
M README.md
A receipts.txtBoth changes are staged, but nothing is committed yet. Navika decides that receipts don't belong in 1.0 after all, and throws the staged changes away:
git cherry-pick --abort 2>/dev/null || git reset --quiet --hard
git status -s
git log --oneline -1b1b9639 (HEAD -> release/1.0) Name the release in the READMEThe status is clean, and the newest commit is still the release README commit. (With -n there is nothing to abort, so git reset --hard cleans up instead. The || runs it when the first command fails.)
Cherry-Picking a Merge Commit
A merge commit has two parents, so Git needs to know which side's changes to copy. git cherry-pick -m 1 <merge> copies everything the merge brought into its first parent, the same rule as git revert -m 1 in "Undoing a Merge". In practice, it is usually clearer to cherry-pick the individual commits of the merged branch instead.
Common Mistakes
Cherry-picking instead of merging. If you need most of a branch, merge or rebase it. Many cherry-picks create duplicate commits that make later merges confusing.
Being on the wrong branch. The copy lands on the current branch. Check git branch --show-current first.
Leaving out -x on release branches. Without it, nobody can tell later that the fix is a copy of a commit on main.
Getting the range wrong. A..B excludes A. Use A^..B to include it.
Cherry-picking a commit that depends on another. The change may not apply without the earlier commit. Pick both, in order.
Under the Hood
Cherry-pick is a three-way merge with an unusual base. To copy commit C with parent P onto HEAD, Git merges with P as the base, C as "theirs" and HEAD as "ours". The result is "the difference from P to C, applied to HEAD". That is why only the commit's own change is copied, and why conflicts look exactly like merge conflicts. Revert uses the same machinery with C as the base and P as "theirs", which applies the opposite change.
Because the copy is a new commit with a new ID, Git sees the original and the copy as different commits. When the branches are merged later, Git usually notices that the same change is already there and doesn't apply it twice, and git rebase skips commits whose change is already present (skipped previously applied commit).
Interview Corner
What does git cherry-pick do?
It applies the changes introduced by one or more existing commits onto the current branch, creating new commits with the same changes and messages but new IDs.
When would you use cherry-pick instead of merge?
When you need only specific commits from another branch, for example backporting a bug fix from main to a release branch, or moving a commit made on the wrong branch. To bring a whole branch, merge or rebase.
What does git cherry-pick -x do?
It appends "(cherry picked from commit
How do you cherry-pick a range of commits?
git cherry-pick A..B copies the commits after A up to B. git cherry-pick A^..B includes A.
How do you handle a conflict during cherry-pick?
Resolve the files, git add them and run git cherry-pick --continue, or use --skip to leave out that commit, or --abort to cancel.
Practice
Challenge: backport the README line. On release/1.0, cherry-pick "Describe receipts" from main with -x. Then print the README and count how many commits on release/1.0 contain "cherry picked from" in their message.
What does Git print? Before you open the solution, decide whether this cherry-pick conflicts with the release title, and how many commits the count finds.
Show solution
git cherry-pick -x ":/Describe receipts"Auto-merging README.md
[release/1.0 338f222] Describe receipts
Date: Mon Jan 5 10:16:00 2026 -0600
1 file changed, 1 insertion(+)No conflict: the commit only adds a line at the end, and the title line it doesn't touch stays as it is on the release. Now the README and the count:
cat README.md
git log --oneline --grep="cherry picked from" | wc -l | tr -d " "Fee Tracker 1.0
Tracks school fees.
Receipts are printed after payment.
2Two commits on release/1.0 were made with -x: the late fee fix and this one. ("Say that fees are per term" was copied without -x.) tr -d " " only removes the spaces some systems put before the number.
Checkpoint. In a practice repository, create a release branch, make three fixes on main mixed with feature commits, and backport only the fixes with git cherry-pick -x. Then create a conflicting change on the release branch and practice both --continue and --abort.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git cherry-pick <commit> | Copy one commit |
git cherry-pick -x <commit> | Copy and record the origin |
git cherry-pick A^..B | Copy a range including A |
git cherry-pick -n <c1> <c2> | Apply several changes without committing |
git cherry-pick --continue | Finish after resolving a conflict |
git cherry-pick --abort | Cancel |
Was this tutorial helpful?