git cherry-pick
Description:
Apply changes from specific commits to your current branch. Useful for selectively bringing changes without merging entire branches.
Syntax:
bash
git cherry-pick <commit-hash>
git cherry-pick <commit1> <commit2>
git cherry-pick --continue
git cherry-pick --abortExamples:
bash
# Apply a single commit
git cherry-pick abc1234
# Apply multiple commits
git cherry-pick abc1234 def5678 ghi9012
# Apply a range of commits
git cherry-pick abc1234..def5678
# Cherry-pick without committing (stage only)
git cherry-pick -n abc1234
# Continue after resolving conflicts
git cherry-pick --continue
# Abort cherry-pick operation
git cherry-pick --abortDetailed Explanation:
- Selective picking: Takes specific commits and applies them to current branch
- New commit hash: Creates a new commit with the same changes but different hash
- Preserves author: Keeps original author information
- Independent of branch: Doesn't require merging the entire source branch
Common Errors:
error: could not apply <commit>→ Solution: Resolve conflicts,git add, thengit cherry-pick --continuefatal: bad revision '<commit>'→ Solution: Check commit hash spelling withgit logerror: your local changes would be overwritten→ Solution: Commit or stash changes firsthint: after resolving conflicts, mark corrected paths with 'git add'→ Solution: Fix conflicts, stage files, continue
Pro Tips:
- Use for hotfixes: cherry-pick a fix from development to production branch
- Find commit hash:
git log --onelinein the source branch - Use
-xto append source commit info:git cherry-pick -x abc1234 - Edit commit message:
git cherry-pick -e abc1234 - Cherry-pick merge commit:
git cherry-pick -m 1 <merge-commit>(specify parent) - Warning: Can create duplicate commits if both branches are eventually merged
Related Commands:
git merge– Integrate entire branchesgit rebase– Reapply series of commitsgit revert– Undo a commitgit log– Find commits to cherry-pick