Cherry-Pick
Cherry-pick applies specific commits from one branch to another, useful for backporting fixes or selectively applying changes.
Basic Cherry-Pick
# 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
# Apply without committing
git cherry-pick -n abc1234
Cherry-Pick Options
# Edit commit message
git cherry-pick -e abc1234
# Add "cherry picked from" to message
git cherry-pick -x abc1234
# Message includes:
# (cherry picked from commit abc1234...)
# Keep original author
git cherry-pick abc1234 # Author preserved by default
# Sign off
git cherry-pick -s abc1234
Handling Conflicts
# When conflicts occur
git cherry-pick abc1234
# CONFLICT (content): Merge conflict in file.js
# Resolve conflicts manually
git status
# Edit conflicting files
# Continue after resolving
git add .
git cherry-pick --continue
# Abort cherry-pick
git cherry-pick --abort
# Skip this commit
git cherry-pick --skip
Cherry-Pick from Another Branch
# Find commits to cherry-pick
git log --oneline feature-branch
# Cherry-pick to current branch
git checkout main
git cherry-pick feature-branch~3..feature-branch
# Cherry-pick specific file changes
git cherry-pick -n abc1234
git reset HEAD
git add specific-file.js
git commit -m "Cherry-pick specific file"
Reverting Commits
Revert creates a new commit that undoes changes, safely reverting without rewriting history.
Basic Revert
# Revert a single commit
git revert abc1234
# Revert without committing
git revert -n abc1234
# Revert multiple commits
git revert abc1234 def5678
# Revert a range (oldest to newest)
git revert abc1234^..def5678
Revert Merge Commits
# Revert a merge commit (must specify parent)
git revert -m 1 merge_commit_hash
# -m 1 means keep changes from first parent (usually main)
# -m 2 means keep changes from second parent (merged branch)
# Check which parent is which
git log --oneline --graph merge_commit_hash^-
Revert Multiple Commits
# Revert last 3 commits
git revert HEAD~3..HEAD
# Revert without creating multiple commits
git revert --no-commit HEAD~3..HEAD
git commit -m "Revert last 3 commits"
# Revert in reverse order to avoid conflicts
git revert HEAD
git revert HEAD~1
git revert HEAD~2
Handling Revert Conflicts
# When revert conflicts
git revert abc1234
# CONFLICT...
# Resolve conflicts
git status
# Edit files
git add .
git revert --continue
# Or abort
git revert --abort
Undoing a Revert
# Revert the revert!
git revert revert_commit_hash
# Or reset if not pushed
git reset --hard HEAD~1
Cherry-Pick vs Revert Use Cases
| Use Cherry-Pick When | Use Revert When |
|---|---|
| Backporting fixes | Undoing pushed changes |
| Moving commits between branches | Rolling back features |
| Selective commit application | Maintaining history integrity |
| Creating hotfixes | Team collaboration |
Practical Examples
# Backport a bugfix to release branch
git checkout release/1.0
git cherry-pick main~2 # Apply fix from main
# Roll back a feature in production
git checkout main
git revert feature-commit-hash
git push origin main
# Apply hotfix to multiple versions
for branch in release/1.0 release/1.1 release/2.0; do
git checkout $branch
git cherry-pick hotfix-commit
done
Best Practices
- Use
-xwith cherry-pick to track origin - Revert instead of reset for shared branches
- Test cherry-picked commits thoroughly
- Document cherry-picks in commit messages
- Consider conflicts when cherry-picking old commits