Git Cherry-Pick Explained: Copy the Commit You Need, Safely, Tested
Git cherry-pick copies the changes from one commit onto your current branch without merging everything else. Learn when to use it, the options that keep history traceable, and how to handle conflicts, with every command run in a real repository.
- Git cherry-pick applies one commit’s changes to the current branch as a new commit with a new hash.
- Use
-xto record where the commit came from, which makes hotfix history traceable. - A range
A..Bexcludes A. UseA^..Bto include it. - On a conflict, fix and run
--continue, or back out completely with--abort.
A bug fix lands on your development branch, but the release branch that customers run needs it today, without the half-finished features sitting next to it. Merging would bring everything. Git cherry-pick lets you take just the fix: it reads the change one commit introduced and applies it on top of your current branch.
We built a small repository and ran every command below with Git 2.56.0 on Windows, using a demo author name and email. Hashes and outputs are copied from that run, so yours will differ. If commits and branches are new to you, start with our Git tutorial for beginners.
What does git cherry-pick do?
Every commit records a snapshot plus a pointer to its parent. Cherry-pick takes the difference between a commit and its parent, applies that difference to your current branch, and creates a new commit with the same message and author. The original commit stays where it was. Nothing is moved; the change is copied.
The scenario
We created an initial commit, branched release-1.0 from it, and then made three commits on a feature branch:
$ git log --oneline feature
201c2ee New UI banner
48ea901 Fix divide-by-zero crash
4335edf Add discount feature
4efe08e Initial release
The release branch needs only 48ea901, the crash fix. The discount feature and new UI are not ready for customers.
A basic cherry-pick
$ git switch release-1.0
$ git cherry-pick 48ea901
[release-1.0 8380813] Fix divide-by-zero crash
$ git log --oneline
8380813 Fix divide-by-zero crash
4efe08e Initial release
$ ls
VERSION app.py util.py
The release branch now has the fix, including the new util.py, and none of the feature files: no discount.py and no ui.py. That is the whole point. Run your tests on the release branch afterwards, because a fix written against newer code may depend on something the old branch lacks.
New hash, same change
The copy got hash 8380813, while the original is 48ea901. A commit hash covers its parent and timestamps, and the copy has a different parent, so the hash must differ. Git can still tell the two commits carry the same change by comparing their patch-id, a hash of the diff itself:
$ git show 48ea901 | git patch-id --stable
dd5f4e371770...
$ git show HEAD | git patch-id --stable
dd5f4e371770...
Identical patch-ids, different commit hashes. Remember this when someone asks “is the fix on release?”: searching for the original hash will not find it.
Checking what is missing with git cherry
$ git cherry -v release-1.0 feature
+ 4335edf... Add discount feature
- 48ea901... Fix divide-by-zero crash
+ 201c2ee... New UI banner
git cherry uses patch-ids to compare branches. A minus sign means an equivalent change already exists on release-1.0; a plus means it does not. That gives you a quick audit of which fixes still need picking.
Recording the source with -x
$ git cherry-pick -x 48ea901
$ git log -1 --format=%B
Fix divide-by-zero crash
(cherry picked from commit 48ea901313c32455d8ce259ba032b311a4740bb5)
-x appends the original full hash to the message. When you maintain several release lines, this line is how you trace a fix back to its source months later. Many teams make it mandatory for hotfixes. Use it when the original commit is on a shared branch; skip it when picking from a private branch that will be deleted, because the reference would point nowhere.
Applying without committing: -n
$ git cherry-pick -n 48ea901
$ git status --short
M app.py
A util.py
$ git log --oneline -1
4efe08e Initial release
-n (or --no-commit) applied and staged the changes but created no commit. Use it to combine several picked commits into one, or to adjust the change before committing.
Tip: before any cherry-pick, check git status. Starting with uncommitted changes makes conflicts much harder to untangle.
Picking a range of commits
$ git cherry-pick 4335edf..feature
$ git log --oneline
60dd132 New UI banner
14c152c Fix divide-by-zero crash
4efe08e Initial release
We asked for 4335edf..feature and got two commits, not three. As in git log, A..B means “commits reachable from B but not from A”, so A itself is excluded. To include A, write 4335edf^..feature. Commits are applied oldest first, which keeps dependent changes in order. This range behaviour also catches people with revert, as our git reset vs revert guide shows.
Handling conflicts
We changed price = 100 to 120 on the release branch, and to 99 in a feature commit called “Promo price”, then tried to pick the promo:
$ git cherry-pick 18fa62a
hint: run "git cherry-pick --abort".
$ git status --short
UU app.py
$ head -5 app.py
<<<<<<< HEAD
price = 120
tax = 0.18
=======
price = 99
Both branches edited the same line, so Git stopped and marked the conflict. You now have three choices:
- Resolve: edit the file,
git add app.py, thengit cherry-pick --continue. - Abort:
git cherry-pick --abortreturns the branch to exactly where it was. After aborting, our file showedprice = 120again. - Skip: in a range,
--skipdrops this commit and continues with the next.
Conflicts during cherry-pick are often a warning sign: the commit depends on changes the target branch does not have. Ask whether you really want this commit there.
Empty picks and –skip
We picked the fix onto a branch that already had it:
$ git cherry-pick 48ea901
The previous cherry-pick is now empty, possibly due to conflict resolution.
$ git cherry-pick --skip
Git noticed the change was already present and refused to make an empty commit. --skip moves on. This happens often when someone has already picked a fix and you do not know it, which is another reason to run git cherry first.
What happens when you merge later?
Cherry-picking leaves the same change in history twice. We merged the feature branch (up to the UI commit) into a branch that already held the picked fix:
* b25922e Merge commit '201c2ee' into empty-demo
|\
| * 201c2ee New UI banner
| * 48ea901 Fix divide-by-zero crash
| * 4335edf Add discount feature
* | cf37c06 Fix divide-by-zero crash
|/
* 4efe08e Initial release
The merge was clean, because both sides made the identical change, and util.py contained the function once. But the log now shows the fix twice under two hashes. When we also merged a later commit that changed the neighbouring price line, Git reported a conflict, because changes on adjacent lines can collide. Duplicate history is harmless in small doses, but heavy cherry-picking between long-lived branches makes merges and history harder to follow. Our merge vs rebase guide covers the alternatives.
When to cherry-pick, and when not to
| Good use | Better alternative |
|---|---|
| Hotfix to a release or maintenance branch | — |
| Rescue one commit from an abandoned branch | — |
| Backport a security fix to older versions | — |
| Moving most of a feature branch | Merge or rebase the branch |
| Shipping unfinished work hidden from users | Feature flags |
| Undoing a commit | git revert |
A common team rule is “fix on the main branch first, then cherry-pick to release”, so the fix is never lost in the next release. Tag the patched release using semantic versioning, for example 1.0.1, and let your CI/CD pipeline run the full test suite on it.
A safe cherry-pick checklist
- Run
git statusand start from a clean working tree. - Run
git cherry -v target sourceto see whether the change is already there. - Pick with
-xwhen the source commit lives on a shared branch. - For several commits, check whether your range should start at
AorA^. - If a conflict looks large,
--abortand ask whether a missing earlier commit is needed first. - Run the tests on the target branch before pushing.
Final thoughts
Git cherry-pick is a precise tool for copying one change to another branch. Use -x for traceability, remember that ranges exclude their first commit, check git cherry before picking, and reach for --abort without fear. Keep it for hotfixes and rescues; for moving whole features, merging stays simpler.
Frequently asked questions
What does git cherry-pick do?
It takes the changes from a chosen commit and applies them to your current branch as a new commit with a new hash. The original commit and branch are not changed.
How do I cherry-pick multiple commits?
List the hashes, or use a range such as A..B. The range excludes A, so use A^..B to include it. Git applies the commits from oldest to newest.
How do I undo or stop a cherry-pick?
During a conflict, run git cherry-pick –abort to return to the previous state. If the pick already finished, use git revert on the new commit, or reset if it was never pushed.
What does git cherry-pick -x do?
It adds a line to the commit message naming the original commit hash, such as cherry picked from commit 48ea901. This keeps backported fixes traceable.
Is cherry-picking bad practice?
No, it is ideal for hotfixes and backports. Heavy use between long-lived branches duplicates history and can complicate later merges, so prefer merging for whole features.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.