Dashboard

What Is Git Cherry-Pick? A Hotfix Example

Git cherry-pick copies a single commit onto the branch you are on. Here is a hotfix backport, the options that matter, and the trap that surprises people later.

Steve Jefferson
Steve Jefferson
Developer Advocate
27 October 20261 min read

Git cherry-pick takes one commit from anywhere in your history and applies the same change on top of the branch you are on, as a brand new commit. You use it when you want one specific fix and not everything around it.

Here is the situation where it earns its keep. You fixed a bug on your main branch on Tuesday. Your customers run version 2.1, which lives on a release-2.1 branch, and main has moved on with features you are not ready to ship. You want the fix on release-2.1 and nothing else. That is a cherry-pick.

A hotfix backport, step by step

Find the commit you want. On main, the log shows it:

sh
git log --oneline main

Say the fix is a1b2c3d. Switch to the release branch and pick it:

sh
git switch release-2.1
git cherry-pick a1b2c3d

Git replays the change from that commit onto release-2.1. The git manual says the command "requires your working tree to be clean", so commit or stash anything in progress first. If the change applies cleanly, you now have a new commit on release-2.1 with the same message and a different ID.

That different ID is the important detail, and we will come back to it.

Options worth knowing

The manual lists many options. These are the ones people actually use:

Option

What it does

When to use it

-x

Appends "(cherry picked from commit ...)" to the message

Backports on shared branches, so readers can trace the origin

-n or --no-commit

Applies the change without committing

Combining several picks into one commit

-m 1

Picks a merge commit, treating parent 1 as the mainline

Cherry-picking a merge, which fails without it

--continue, --skip, --abort

Controls a pick that stopped on a conflict

After resolving, skipping, or cancelling

Two details from the manual are easy to miss. First, the manual says -x is done "only for cherry picks without conflicts", and warns against it on private branches because the recorded hash is useless to anyone who cannot see that branch. Second, with -n the eventual commit is authored by you, not by the original author.

Picking more than one commit

You can list commits, or use a range:

sh
git cherry-pick a1b2c3d e4f5a6b
git cherry-pick a1b2c3d..e4f5a6b

The range form A..B leaves out A itself and includes B. If you want both ends, write A^..B. The manual notes that when you give a range, git feeds everything into a single revision walk, so ordering follows history, not the order you typed.

When a pick hits a conflict

Sometimes the code around the fix is different on the target branch, and git cannot apply it cleanly. The manual describes what happens: the branch stays at the last commit that succeeded, CHERRY_PICK_HEAD points at the commit that stopped, and conflicting files get conflict markers. Nothing else is touched.

Your options are:

  1. Fix the files, git add them, then git cherry-pick --continue.

  2. Run git cherry-pick --skip to drop that commit and move on.

  3. Run git cherry-pick --abort to return to the state before the whole sequence began.

If a conflict feels bigger than the fix, --abort is a fine answer. The commit may depend on earlier work that the release branch does not have, and picking it alone was the wrong plan.

The duplicate-commit trap

Cherry-picking copies the change, not the commit. So after the hotfix pick, the same fix exists twice in your repository, once on main with ID a1b2c3d and once on release-2.1 with a new ID. Git does not know they are related.

Usually that is harmless. Later, when you merge release-2.1 back into main, git compares the actual file contents, sees the same change on both sides, and generally resolves it without fuss. It gets messy when someone also edited those lines differently in between, and you get a conflict on a change you thought you had already handled.

The habits that keep this clean:

  • Use -x on shared branches so the origin is written down.

  • Cherry-pick bug fixes, not features that depend on other commits.

  • Do not cherry-pick the same change back and forth between branches.

Cherry-pick, merge, or rebase

You want to

Use

Bring one fix to a release branch

Cherry-pick

Bring all of a finished branch into main

Merge, usually through a pull request

Tidy your own branch before sharing it

Rebase

Share a fix across two long-lived branches regularly

Rethink the branch layout

If you find yourself cherry-picking five or six commits every week, that is a sign the two branches have drifted apart and a different branching approach would save time.

Cherry-pick and AI coding agents

An agent will happily cherry-pick if you ask, and it is usually correct on clean picks. Where it goes wrong is the conflict case. It may resolve a conflict by guessing, or pick a commit that depends on earlier work. Two rules help. Ask it to show you the commit with git show before picking, and review the result as a diff, the same way you would review any agent's git diff before merging. The guide to merge conflicts with AI coding agents covers the conflict half in more depth, and the AI coding tools overview puts all of this in context.

FAQ

What does git cherry-pick do?

It applies the changes from one or more existing commits onto your current branch and records a new commit for each one.

Does cherry-pick delete the original commit?

No. The original stays where it was. You get a copy with a new ID on the current branch.

How do I cherry-pick a merge commit?

Add -m with the parent number to treat as the mainline, for example git cherry-pick -m 1 <merge-commit>. Without it, git refuses because it cannot tell which side to keep.

How do I undo a cherry-pick?

If it is still in progress, run git cherry-pick --abort. If it already committed, you can revert the new commit or reset the branch, but check git reflog before resetting so you do not lose other work.

Is cherry-pick bad practice?

Not by itself. It is the right tool for backporting a fix. It becomes a problem when it is used to move large amounts of work between branches that should have been merged.

How did this land?

About the author

Steve Jefferson
Steve Jefferson

Developer Advocate

Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.