Cherry-Picking Commits
Cherry-picking applies a specific commit from one branch into another. It’s useful for pulling in a single fix without merging entire branches.
Basic Usage
git cherry-pick <commit-hash>
This creates a new commit on your current branch with the same changes as the original. The original commit remains untouched on its branch.
Cherry-Pick a Range
git cherry-pick A..B
Applies commits from A (exclusive) to B (inclusive). Useful for picking a batch of related changes.
Cherry-Pick Without Committing
git cherry-pick --no-commit <commit-hash>
Applies the changes to your working directory without creating a commit. Lets you review before staging and committing manually.
Multiple Commits
git cherry-pick <hash1> <hash2> <hash3>
You can cherry-pick multiple specific commits by listing their hashes. Each creates a new commit on the current branch.
Finding Commits to Cherry-Pick
Use git log to find the commit hash you want:
git log --oneline feature-branch
# Output:
# a1b2c34 Fix login validation
# d5e6f78 Add error handling
# g9h0i12 Update user model
When to Use Cherry-Pick
- Backporting a bugfix to a release branch
- Pulling one feature commit into a different branch
- Recovering a commit that was lost during a rebase
- Applying a hotfix to multiple active branches
Handling Conflicts
If the cherry-pick causes a conflict, Git stops and asks you to resolve it:
# After resolving conflicts:
git add .
git cherry-pick --continue
# Or abort:
git cherry-pick --abort
Best Practices
- Prefer merging when pulling multiple commits from the same branch
- Use cherry-pick for isolated fixes or specific backports
- Always verify the cherry-picked commit with
git diff - Note the original commit hash in the message for traceability
Common Mistakes
- Cherry-picking too many commits — at that point, merge is cleaner
- Not realizing cherry-pick creates a new commit with a different hash
- Cherry-picking commits that depend on other unapplied commits
- Forgetting to resolve conflicts during the cherry-pick
- Cherry-picking across branches with very different histories