Skip to main content

What is Git Cherry-pick?

git cherry-pick copies the change from a commit you name onto the branch you have checked out.

You use it when one commit belongs on a second branch but the rest of its source branch does not. The classic case is a bug fix on main that a release branch also needs. Every example on this page was run against Git 2.43.

Introduction​

A Git commit stores a snapshot and a pointer to its parent. Cherry-pick takes the difference between a commit and its parent, applies that difference to HEAD, and records the result as a new commit. The source commit and its branch stay exactly as they were.

Definition

Cherry-pick

The Git operation that applies the changes introduced by selected commits to the current branch, creating new commits.

The new commit has a different object ID because its parent is different. Git keeps the original author and message, and records you as the committer. Read What is Version Control? if commit IDs and parents are new to you.

Syntax​

Switch to the branch that should receive the change, then name the commit:

git switch release/2.3
git cherry-pick a3f9c12

You can name several commits. Git applies them in the order you list them:

git cherry-pick a3f9c12 7be41d0

A range uses the same A..B notation as git log. It applies every commit reachable from B but not from A, so A itself is left out. Add ^ to include it:

CommandCommits applied
git cherry-pick A..BThe commits after A, up to and including B
git cherry-pick A^..BA through B, both included

Check the range with git log --oneline A^..B before you apply it, so you know which commits you are about to copy.

Useful Options​

OptionEffect
-xAppends (cherry picked from commit <id>) to the message
-n, --no-commitApplies the changes to the index and working tree without committing
-e, --editOpens the commit message in your editor before committing
-m <parent>Picks a merge commit, using the numbered parent as the base

Use -x when the source commit is already public, for example on main. The trailer tells anyone reading the release branch where the change came from. Skip it for commits on a private branch, because the recorded ID will point to a commit nobody else can see.

Use -n to fold several commits into one. git cherry-pick -n a3f9c12 7be41d0 stages both changes, and you write a single commit with git commit.

Git refuses a merge commit without -m and reports commit <id> is a merge but no -m option was given. -m 1 applies the changes the merge brought in relative to its first parent, which is usually the branch that was merged into.

Resolving Conflicts​

A conflict means the change does not apply cleanly to the destination branch, usually because the surrounding lines differ there. Git stops and prints:

CONFLICT (content): Merge conflict in a.txt
error: could not apply 326da25... main edits a

You then have three ways out:

# Fix the files, stage them, and let Git create the commit
git add a.txt
git cherry-pick --continue

# Drop this commit and move on to the next one in a range
git cherry-pick --skip

# Undo the whole cherry-pick and return to where you started
git cherry-pick --abort

--skip also handles a commit whose change is already on the branch. Git reports that the cherry-pick is now empty, and --skip moves past it. If the same conflict keeps coming back across release branches, turn on Git rerere so Git reuses your earlier resolution. The zdiff3 conflict style shows the original lines inside each conflict, which makes the resolution easier to read.

When to Use Cherry-pick​

Cherry-pick fits a change that stands on its own:

  • Backports: copy a fix from main to one or more release branches.
  • Misplaced commits: move a commit made on the wrong branch to the right one.
  • One fix from a busy branch: take a single commit from a branch that is not ready to merge.

It does not fit when you want the whole branch. Replaying a whole series of commits onto a new base is the job of git rebase. Cherry-pick vs Rebase compares the two in detail. To bring two branches that are meant to converge together, use a merge or a rebase instead of copying commits between them.

A cherry-picked commit depends on the code around it. If the fix relies on an earlier commit that the destination branch lacks, pick that commit first, in dependency order.

Next Steps​