Coding

Git Cherry-Pick: Copy a Commit to Another Branch (With Examples)

git cherry-pick <commit> copies the changes from one existing commit onto the branch you have checked out, as a new commit. Switch to the branch that should receive the change, find the commit's hash with git log --oneline, then run git cherry-pick 3d05ee7.

git switch main
git log --oneline feature
git cherry-pick 3d05ee7

The typical use is moving a single fix without merging everything else: a bug fix made on a feature branch that also needs to go to main or a release branch today.

A worked example

Here a feature branch has two commits, and only the first (a bug fix) should go to main. This was run with Git 2.45; your hashes will differ.

$ git log --oneline feature
a724ed2 Add feature B
3d05ee7 Fix null check in parser
29e1148 Initial commit

$ git switch main
$ git cherry-pick -x 3d05ee7
[main 63d37ff] Fix null check in parser
 Date: Mon Oct 26 13:33:17 2026 +0200
 1 file changed, 1 insertion(+)
 create mode 100644 fix.txt

The new commit on main is 63d37ff, not 3d05ee7. A cherry-pick is a copy: same change, same message, same author, but a new hash because it has a different parent. The original commit is untouched on feature.

What -x does (and when to use it)

-x appends a line recording where the commit came from:

$ git log -1 --format=%B
Fix null check in parser

(cherry picked from commit 3d05ee7fd540ed33c430419e3bd6532103971a9d)

Use it when backporting from a public branch to a release branch, so anyone can trace the fix back. The Git docs advise against it when picking from your own private branch, because the referenced commit is useless to anyone who can't see it. Git only adds the line for cherry-picks without conflicts.

How to cherry-pick several commits or a range

CommandWhat it applies
git cherry-pick A B CThose three commits, in the order given
git cherry-pick A..BCommits after A up to and including B (A itself is not included)
git cherry-pick A^..BA through B, including A
git cherry-pick main..featureEvery commit on feature that isn't on main

The A..B exclusion catches many people: it means "reachable from B but not from A", so the starting commit is left out. Each commit in a range becomes its own new commit:

$ git cherry-pick main..feature
[main 1874775] Fix null check in parser
[main 3bd4503] Add feature B

Cherry-pick without committing (--no-commit)

git cherry-pick -n <commit> (long form --no-commit) applies the changes to your working tree and index but doesn't commit. That is useful to combine several picked commits into one, or to adjust the change before committing:

git cherry-pick -n 3d05ee7 9b1c2aa
git commit -m "Backport parser fixes"

Without -n, -e (--edit) lets you edit the commit message before Git commits.

How to resolve a cherry-pick conflict

If the change doesn't apply cleanly, Git stops and tells you so:

Auto-merging app.txt
CONFLICT (content): Merge conflict in app.txt
error: could not apply a724ed2... Add feature B
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".

Your branch stays at the last successful commit. Files that applied cleanly are already staged; conflicting files contain <<<<<<< and >>>>>>> markers. You then have four options:

  • Fix and continue: edit the files, git add app.txt, then git cherry-pick --continue.
  • Skip this commit: git cherry-pick --skip drops it and moves on to the next commit in the sequence.
  • Abort everything: git cherry-pick --abort returns the branch to exactly where it was before you started.
  • Stop but keep what's done: git cherry-pick --quit clears the in-progress state and keeps the commits already made.

Error: commit is a merge but no -m option was given

Cherry-picking a merge commit fails by default:

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

A merge has two parents, and Git doesn't know which side's changes you want. -m 1 tells it to replay the merge relative to its first parent (normally the branch that was merged into), which brings over everything the merged branch added:

git cherry-pick -m 1 58162dc

Often it is cleaner to cherry-pick the individual commits from the merged branch instead.

The previous cherry-pick is now empty

If the change is already on your branch, Git stops with:

The previous cherry-pick is now empty, possibly due to conflict resolution.

Nothing is lost; there is just nothing new to commit. Run git cherry-pick --skip. To keep such commits automatically, pass --empty=keep, or --empty=drop to discard them without stopping. --allow-empty only keeps commits that were empty in the first place.

When not to cherry-pick

Cherry-picking duplicates a change under a new hash. That is fine for a one-off fix, but if you find yourself picking most of a branch, merge or rebase it instead; the trade-offs are covered in git merge vs rebase. Picking a commit that depends on earlier, unpicked commits also tends to conflict, because the code it modifies isn't there yet.

If it still goes wrong

If a cherry-pick finished but produced the wrong result and you haven't pushed, git reset --hard HEAD~1 removes the new commit (commit or stash other work first). If you are unsure which commits Git thinks are already applied, git log --oneline main..feature lists what feature has that main doesn't, and git status shows whether a cherry-pick is still in progress. For more ways to back out a commit, see how to undo the last Git commit.

Frequently asked questions

Does git cherry-pick move the commit or copy it?
It copies it. Cherry-pick creates a new commit on your current branch with the same changes and message but a different hash, because its parent is different. The original commit stays where it was on its own branch.
Will cherry-picking cause duplicate commits when I merge later?
You will have two commits with the same change and different hashes. Git usually merges this cleanly because the content is identical, but the history shows the change twice. If the original branch is going to be merged soon anyway, waiting for the merge avoids the duplicate.
How do I undo a cherry-pick I already finished?
If you have not pushed it, git reset --hard HEAD~1 removes the new commit (and discards uncommitted changes, so commit or stash those first). If you have pushed it, use git revert followed by the new commit's hash, which adds a commit that reverses it without rewriting shared history.
Can I cherry-pick from another repository or a fork?
Yes, once the commit is in your local repository. Add the other repository as a remote with git remote add, run git fetch on that remote, then cherry-pick the commit by its hash.

Sources