Picking Individual Commits
Merging brings over an entire branch. Sometimes you only need one commit—a single bug fix or a specific improvement. git cherry-pick copies an individual commit from one branch and applies it to another.
Basic Cherry-Pick
# Find the commit you want
git log --oneline feature-branch
# Apply it to your current branch
git cherry-pick abc123Git creates a new commit on your current branch with the same changes and message. The new commit has a different hash because it has a different parent.
Cherry-Picking Multiple Commits
# Pick several specific commits
git cherry-pick abc123 def456
# Pick a range (older..newer, excludes abc123)
git cherry-pick abc123..ghi789Common Use Cases
- Hotfixes: Apply a critical bug fix to a release branch without merging unfinished work
- Backporting: Bring a fix from the latest version to an older, supported version
- Rescuing work: Recover a good commit from an abandoned branch
Useful Options
git cherry-pick -x abc123 # Record the original commit hash in the message
git cherry-pick --no-commit abc123 # Apply changes without committing
git cherry-pick --edit abc123 # Edit the commit message before committingThe -x flag is especially useful on shared branches—it documents where the change came from.
Handling Conflicts
Cherry-picks can conflict just like merges. Resolve them the same way:
- Fix the conflicts in the affected files
- Stage them with
git add - Continue with
git cherry-pick --continue
Or abandon the operation entirely:
git cherry-pick --abortA Word of Caution
Duplicate Commits Ahead
Cherry-picking creates a copy of a commit, not a link to it. If you later merge the original branch, the same change may appear twice in your history. Prefer merging or rebasing for routine integration—reserve cherry-pick for targeted, surgical updates.
Best Practices
- Cherry-pick single, self-contained commits—if a fix spans several commits, consider a range instead
- Always check
git log -pon the source commit first, so you know exactly what you're bringing over - After a cherry-pick lands on a release branch, note it in your changelog so the next merge doesn't cause confusion
Next: Temporal Storage
You can now transplant individual commits between timelines. Next, learn git stash—a place to safely park unfinished work while you switch contexts.