git cherry-pick

Branching and Merging

Apply changes from specific commits onto the current branch


The 'git cherry-pick' command applies the changes introduced by one or more existing commits onto the current branch, creating new commits for each. Unlike merge or rebase which work with entire branches, cherry-pick lets you selectively copy specific commits. This is invaluable for backporting hotfixes to release branches, recovering lost commits, or bringing specific features from one branch to another without merging everything. Each cherry-picked commit gets a new SHA - it's a copy, not a move. Cherry-pick is surgical and precise, giving you fine-grained control over which changes land on your branch.

Synopsis

git cherry-pick [--edit] [-n] [-m <parent-number>] [-s] [-x] [--ff] [-S[<keyid>]] <commit>...
git cherry-pick (--continue | --skip | --abort | --quit)
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (2)

Apply changes to working directory and index but don't create a commit. Perfect for combining multiple cherry-picks into one commit.

When neededCombine several picks into one commit

Example

git cherry-pick -n abc1234

Open editor to modify the commit message before committing. Use when you want to add context about why the commit was cherry-picked.

When neededAdds context to the copied commit's message

Example

git cherry-pick -e abc1234

Intermediate (5)

Resume cherry-pick after resolving conflicts. You must stage resolved files with 'git add' first.

RecommendedResumes after you resolve and stage the conflicts

Example

git cherry-pick --continue

Cancel cherry-pick operation and restore branch to pre-cherry-pick state. Use when conflicts are too complex or you picked the wrong commit.

RecommendedSafely cancels and restores the pre-cherry-pick state

Example

git cherry-pick --abort

Skip the current commit and continue with remaining commits in the sequence. The skipped commit's changes are NOT applied.

When neededSkips a commit whose changes you no longer need

Example

git cherry-pick --skip

Append '(cherry picked from commit ...)' to the commit message. Helps track the origin of cherry-picked commits.

RecommendedRecords the source commit — essential for backports

Example

git cherry-pick -x abc1234

Add 'Signed-off-by' trailer with your name and email. Required by some projects for legal/audit compliance.

When neededOnly if the project requires Signed-off-by trailers

Example

git cherry-pick -s abc1234

Advanced (2)

Keep commits that were empty to begin with (same tree as their parent). Commits that become empty because their changes already exist still stop; use --empty=keep for those.

When neededOnly for commits that were empty in the first place

Example

git cherry-pick --allow-empty abc1234

Required when cherry-picking merge commits. Specifies which parent (1 or 2) is the mainline. Usually -m 1 to get the merged branch's changes.

When neededRequired for merge commits; usually -m 1

Example

git cherry-pick -m 1 abc1234

Expert (2)

Use the specified merge strategy when applying changes. Default is 'ort' (or 'recursive' on older Git). There is no short form: -s means --signoff here.

Not recommendedThe default ort strategy is almost always right

Example

git cherry-pick --strategy=ort abc1234

Pass options to the merge strategy. Common: 'theirs' (favor picked changes), 'ours' (favor current), 'patience' (better diffs).

Not recommended-Xours / -Xtheirs silently discard one side of conflicting hunks

Example

git cherry-pick -Xtheirs abc1234

Choosing the Right Option

Comparing git cherry-pick with related commands

Understanding when to use cherry-pick vs merge, rebase, or revert

CommandAspectCherry-PickOther Command
ScopeSpecific commits onlyMerge: entire branch / Rebase: whole branch
Creates new commitsYes - new SHA, same contentMerge: merge commit / Rebase: rewrites all
Original staysYes - original untouchedMerge: yes / Rebase: orphaned
Use caseBackporting, selective portingIntegrating complete branches
History effectCreates duplicate changesPreserves or linearizes
ConflictsPer commit during pickAt merge point or per rebase commit

Workflow Examples

1
git checkout release-1.0

Switch to the release branch

2
git log main --oneline

Find the commit hash of the fix

3
git cherry-pick -x abc123

Apply the fix with origin reference

4
git push origin release-1.0

Push the backported fix

1
git checkout main

Switch to main branch

2
git log feature --oneline

Review commits on feature branch

3
git cherry-pick abc123 def456

Pick only the ready commits

4
git push origin main

Push the selected commits

1
git reflog

Find the commit hash in reflog

2
git checkout main

Switch to target branch

3
git cherry-pick abc123

Apply the orphaned commit

1
git checkout target-branch

Switch to destination branch

2
git cherry-pick A^..C

Apply commits A through C (inclusive)

3
git log --oneline

Verify commits were applied

Troubleshooting

"commit is a merge but no -m option was given"

Cause: You're trying to cherry-pick a merge commit without specifying which parent

Use -m 1 or -m 2 to specify the mainline parent

git cherry-pick -m 1 <merge-commit>

Conflicts during cherry-pick

Cause: The changes conflict with your current branch

Resolve conflicts in the files, git add them, then git cherry-pick --continue. Or abort with --abort

git add <resolved-files> && git cherry-pick --continue

"The previous cherry-pick is now empty"

Cause: The commit's changes already exist in your branch

Run 'git commit --allow-empty' to record it anyway, or 'git cherry-pick --skip' to skip it

git cherry-pick --skip

Cherry-pick created duplicate commits

Cause: The same changes now exist as different commits on different branches

This is expected behavior. Use -x flag to document the relationship

git cherry-pick -x <commit>

Wrong commit was cherry-picked

Cause: Picked the wrong commit SHA

If not pushed: git reset --hard HEAD~1. If pushed: git revert the cherry-picked commit

git reset --hard HEAD~1

Cherry-pick seems to have no effect

Cause: The commit might be empty or changes already applied

Check git diff HEAD~1 to see actual changes

git diff HEAD~1

Tips & Pitfalls

Best Practices

  • ✓Use -x for backports to add '(cherry picked from commit ...)' reference in commit message
  • ✓Cherry-pick in chronological order - dependent commits must be applied oldest-to-newest to avoid conflicts
  • ✓Prefer merge for shared branches - cherry-pick creates duplicate commits while merge preserves history
  • ✓Test after cherry-picking - the commit might work in its original context but fail in the new one
  • ✓Document cherry-picks in commit messages using -e flag to add context about why the commit was copied
  • ✓Use --no-commit to combine related picks into a single cleaner commit
  • ✓Consider using git cherry instead of log to find commits that haven't been applied yet
  • ✓When backporting, always include the original commit hash in the message for traceability

Common Mistakes

  • ✗Forgetting -m for merge commits. Cherry-picking a merge commit without '-m 1' or '-m 2' fails - you must specify which parent to diff against.
  • ✗Not staging resolved files before --continue. You must run 'git add' on resolved files before 'git cherry-pick --continue'.
  • ✗Expecting the same commit hash. Cherry-pick creates NEW commits with new SHAs - it copies changes, not moves commits.
  • ✗Cherry-picking commits that are already applied. This causes conflicts or empty commits - check with 'git log' first.
  • ✗Picking commits in wrong order. Dependent commits must be picked from oldest to newest, otherwise you'll get conflicts.
  • ✗Using cherry-pick on public branches creates duplicate commits when others have the original - prefer merge for shared branches.

FAQ

A:

No. Cherry-pick creates a NEW commit with the same changes but a different SHA. The original commit stays where it was.

A:

Git commit hashes are based on content, parent, author, date, and message. Cherry-pick keeps the author and author date, but the parent and committer date differ, so the hash changes even if the changes are identical.

A:

Yes! List them: git cherry-pick A B C, or use ranges: git cherry-pick A^..C (includes A and C).

A:

Use the -m flag: git cherry-pick -m 1 <merge-commit>. The number specifies which parent is the mainline (usually 1).

A:

A..B excludes A (commits after A up to B). A^..B includes A (commits from A to B inclusive).

A:

If not pushed: git reset --hard HEAD~1. If pushed: git revert <commit> to create an undo commit.

A:

Cherry-pick for specific commits. Rebase for moving entire branches. If you need most commits from a branch, rebase is cleaner.