import DefinitionCard from "@site/src/components/DefinitionCard";

# 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.

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?](./version-control.mdx) if commit IDs and parents are new to you.

## Syntax

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

```bash
git switch release/2.3
git cherry-pick a3f9c12
```

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

```bash
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:

| Command | Commits applied |
| --- | --- |
| `git cherry-pick A..B` | The commits after `A`, up to and including `B` |
| `git cherry-pick A^..B` | `A` 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

| Option | Effect |
| --- | --- |
| `-x` | Appends `(cherry picked from commit )` to the message |
| `-n`, `--no-commit` | Applies the changes to the index and working tree without committing |
| `-e`, `--edit` | Opens the commit message in your editor before committing |
| `-m ` | 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  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:

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

You then have three ways out:

```bash
# 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](./git-rerere.mdx) so Git reuses your earlier resolution. The [zdiff3 conflict style](./zdiff3.mdx) 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](./cherry-pick-vs-rebase.mdx) compares the two in detail. To bring two branches that are meant to converge together, use a [merge or a rebase](./merge-vs-rebase.mdx) 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

- [Cherry-pick vs Rebase](./cherry-pick-vs-rebase.mdx): choose between copying selected commits and replaying a branch
- [What is the Git Reflog?](./git-reflog.mdx): recover the branch state from before a cherry-pick went wrong
- [Merge vs Rebase](./merge-vs-rebase.mdx): compare the two ways to integrate diverged branches
- [What is Git Rerere?](./git-rerere.mdx): reuse conflict resolutions across repeated backports
