git cherry-pick
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)Options
Choosing the Right Option
Comparing git cherry-pick with related commands
Understanding when to use cherry-pick vs merge, rebase, or revert
| Command | Aspect | Cherry-Pick | Other Command |
|---|---|---|---|
Scope | Specific commits only | Merge: entire branch / Rebase: whole branch | |
Creates new commits | Yes - new SHA, same content | Merge: merge commit / Rebase: rewrites all | |
Original stays | Yes - original untouched | Merge: yes / Rebase: orphaned | |
Use case | Backporting, selective porting | Integrating complete branches | |
History effect | Creates duplicate changes | Preserves or linearizes | |
Conflicts | Per commit during pick | At merge point or per rebase commit |
Workflow Examples
git checkout release-1.0Switch to the release branch
git log main --onelineFind the commit hash of the fix
git cherry-pick -x abc123Apply the fix with origin reference
git push origin release-1.0Push the backported fix
git checkout mainSwitch to main branch
git log feature --onelineReview commits on feature branch
git cherry-pick abc123 def456Pick only the ready commits
git push origin mainPush the selected commits
git reflogFind the commit hash in reflog
git checkout mainSwitch to target branch
git cherry-pick abc123Apply the orphaned commit
git checkout target-branchSwitch to destination branch
git cherry-pick A^..CApply commits A through C (inclusive)
git log --onelineVerify 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 --skipCherry-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~1Cherry-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~1Tips & 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
No. Cherry-pick creates a NEW commit with the same changes but a different SHA. The original commit stays where it was.
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.
Yes! List them: git cherry-pick A B C, or use ranges: git cherry-pick A^..C (includes A and C).
Use the -m flag: git cherry-pick -m 1 <merge-commit>. The number specifies which parent is the mainline (usually 1).
A..B excludes A (commits after A up to B). A^..B includes A (commits from A to B inclusive).
If not pushed: git reset --hard HEAD~1. If pushed: git revert <commit> to create an undo commit.
Cherry-pick for specific commits. Rebase for moving entire branches. If you need most commits from a branch, rebase is cleaner.