Cherry Pick Commits in Git
Cherry picking is a powerful Git feature that allows you to apply specific commits from one branch to another. This guide will show you how to use cherry picking effectively to manage your codebase.
What You’ll Learn
- What is cherry picking
- When to use cherry picking
- How to cherry pick commits
- Handling conflicts
- Best practices
Implementation Steps
-
Basic Cherry Pick
# Cherry pick a specific commit git cherry-pick <commit-hash>
- Applies commit to current branch
- Creates new commit with same changes
- Preserves commit message
- Maintains authorship
-
Multiple Commits
# Cherry pick range of commits git cherry-pick <start-commit>..<end-commit> # Cherry pick multiple specific commits git cherry-pick <commit1> <commit2> <commit3>
- Apply multiple commits
- Maintain commit order
- Handle conflicts
- Preserve history
-
Cherry Pick Options
# Keep original commit message git cherry-pick -x <commit-hash> # Skip commit if already applied git cherry-pick --skip # Abort cherry pick git cherry-pick --abort
- Control commit messages
- Handle duplicates
- Manage conflicts
- Recover from errors
-
Conflict Resolution
# Resolve conflicts git add resolved-files git cherry-pick --continue # Skip conflicting commit git cherry-pick --skip
- Handle merge conflicts
- Continue after resolution
- Skip problematic commits
- Maintain clean history
Best Practices
-
When to Cherry Pick
- Apply hotfixes to multiple branches
- Backport features
- Fix specific issues
- Share changes between branches
-
Cherry Pick Strategy
- Pick related commits together
- Maintain commit order
- Test after cherry picking
- Document picked commits
-
Conflict Management
- Resolve conflicts carefully
- Test after resolution
- Document changes
- Keep history clean
-
Safety Measures
- Create backup branch
- Test before pushing
- Document picked commits
- Communicate with team
Common Use Cases
-
Hotfix Application
# Apply hotfix to multiple branches git checkout main git cherry-pick hotfix-commit git checkout develop git cherry-pick hotfix-commit
-
Feature Backporting
# Backport feature to older version git checkout release-1.0 git cherry-pick feature-commits
-
Bug Fix Sharing
# Share bug fix between branches git cherry-pick bugfix-commit
-
Selective Updates
# Pick specific changes git cherry-pick commit1 commit2
Advanced Usage
-
Cherry Pick with Rebase
# Rebase and cherry pick git rebase -i HEAD~3 git cherry-pick <commit>
-
Cherry Pick with Conflicts
# Handle conflicts git cherry-pick <commit> # Resolve conflicts git add resolved-files git cherry-pick --continue
-
Cherry Pick with Hooks
# Pre-cherry-pick hook #!/bin/sh # Add validation logic
-
Cherry Pick with Reflog
# Recover from mistakes git reflog git reset --hard HEAD@{1}
Common Issues and Solutions
-
Merge Conflicts
# Resolve conflicts git add resolved-files git cherry-pick --continue
-
Duplicate Commits
# Skip if already applied git cherry-pick --skip
-
Wrong Commit
# Abort cherry pick git cherry-pick --abort
Conclusion
Cherry picking is a powerful tool for managing code across branches. Remember to:
- Use cherry pick appropriately
- Handle conflicts carefully
- Test after cherry picking
- Document picked commits
- Follow team standards
Next Steps
After mastering cherry picking, you might want to:
- Learn about Git workflows
- Explore Git rebase
- Study Git hooks
- Understand Git internals