Git Cherry-Pick: Copying a Commit to Another Branch
Cherry-pick applies one commit to another branch. Learn the syntax, the conflicts, the ranges, and when a merge is better.
By Velloreth
What it does
It applies the change from one commit to the current branch as a new commit.
git switch release
git cherry-pick abc123
The new commit has a different hash, because the parent is different.
The original commit is unchanged.
The syntax
# One commit
git cherry-pick abc123
# Several commits
git cherry-pick abc123 def456
# A range, exclusive of A
git cherry-pick A..B
# A range, inclusive of A
git cherry-pick A^..B
# Without committing, to review
git cherry-pick -n abc123
# Record the source commit
git cherry-pick -x abc123
-n stages the change without committing, which is useful to combine several.
-x appends (cherry picked from commit abc123) to the message.
The conflict
A conflict is resolved like a merge conflict.
# A conflict occurs
# Edit the file, then:
git add the-file
git cherry-pick --continue
# Or abandon
git cherry-pick --abort
--abort restores the branch to its state before the cherry-pick.
--skip skips the current commit and continues with the next in a range.
The use cases
A hotfix on a release branch.
git switch main
git cherry-pick abc123 # the fix
git switch release-1.2
git cherry-pick abc123 # the same fix
A single commit from a feature branch that is not ready to merge.
A commit that was made on the wrong branch.
git switch correct-branch
git cherry-pick abc123
git switch wrong-branch
git reset --hard HEAD~1
A backport to an older version.
The duplicate problem
The new commit has a different hash, so Git does not recognise it as the same change.
When the branches are later merged, the same change may appear twice.
The result: a conflict, or a duplicate change that is applied twice.
The fix: a merge or a rebase when the whole branch is wanted.
A note in the message with -x makes the duplicate traceable.
Cherry-pick versus merge versus rebase
| Cherry-pick | Merge | Rebase | |
|---|---|---|---|
| Copies | one commit | the whole branch | the whole branch |
| History | diverges | a merge commit | linear |
| Duplicate risk | high | low | low |
| Use | a single fix | integrating a branch | a clean history |
A merge integrates the whole branch and preserves the history.
A rebase replays the whole branch onto another base.
A cherry-pick copies one commit, and it is the wrong tool for a whole branch.
The workflow
A backport workflow:
# Find the commit on main
git log --oneline main | head
# Switch to the release branch
git switch release-1.2
# Cherry-pick with the source recorded
git cherry-pick -x abc123
# Push
git push origin release-1.2
A conflict is resolved as above.
A test after the cherry-pick, because the context differs from the original branch.
The interactive rebase
A cherry-pick is often used where an interactive rebase is the better tool. An interactive rebase can reorder, squash, edit and drop commits, and it can move a set of commits to another branch in one operation. The command is git rebase -i <base>, which opens an editor with a list of the commits and a command per line. The commands are pick, reword, edit, squash, fixup, drop and exec. For moving a single commit, a cherry-pick is simpler; for moving several, an interactive rebase is cleaner because it produces a linear history without the duplicate-commit problem. The rule of thumb is that a cherry-pick copies a change and a rebase moves it, and the difference matters when the branches are later merged.
Recovering from a mistake
A cherry-pick that goes wrong is recoverable, and knowing the recovery is what makes the command safe to use. The first case is a conflict: resolve the file, stage it, and continue, or abort to restore the branch. The second is a wrong commit: reset the branch to the previous state with git reset --hard HEAD~1, which discards the cherry-pick, or use git revert to add a commit that undoes it, which is the safer choice on a shared branch. The third is a cherry-pick of the wrong commit: the same reset or revert applies. The fourth is a half-applied range: --abort restores the branch, and --skip moves past the current commit. The fifth is a duplicate that was already pushed: a revert is the only safe option, because a reset on a shared branch rewrites history that others have. Knowing which of those applies before running the command is the difference between a quick fix and a lost afternoon.
The backport workflow in a team
A backport is a process as much as a command, and the process is what keeps a release branch trustworthy. The first rule is that a fix lands on the main branch first, and the backport is a copy of that commit rather than a separate fix. The second is that the backport is recorded with -x, so the source commit is traceable from the release branch. The third is that the backport is tested on the release branch, because the context differs and a fix that works on main may not apply cleanly. The fourth is that the release branch is protected, so a backport goes through a pull request and a review rather than a direct push. The fifth is that the release notes name the backported fixes, so a user of the release knows what changed. A team that follows that process has a release branch that can be trusted, and a team that cherry-picks ad hoc has a branch whose contents nobody can predict.
Common mistakes
A cherry-pick of a whole branch. A diverged history and duplicates.
No test after the cherry-pick. The context differs.
A conflict resolved by taking one side. A lost change.
No -x. The source is not traceable.
A cherry-pick of a merge commit. It needs -m to select the parent.
A cherry-pick onto a branch with a different structure. A conflict or a wrong result.
A duplicate commit left in place. A confusing history.
A cherry-pick used instead of a merge. The wrong tool.
No --abort when it goes wrong. A half-applied state.
A quotable summary
git cherry-pick applies the change from one commit to the current branch as a new commit with a different hash, which is why it is used for a single fix such as a hotfix on a release branch. A range copies several commits, -x records the source, and a conflict is resolved like a merge conflict with --continue or --abort. Prefer a merge or a rebase when the whole branch is wanted, because a cherry-pick produces a diverged history and duplicate commits.
Related reading
For the related topics, see git-rebase-vs-merge and git-branching-strategies. For the broader context, git-commit-messages-guide and conventional-commits-guide.