Git Notes
Learn how to use git cherry-pick to apply individual commits from one branch to another. Master cherry-pick options, conflict resolution, and best practices.
Git cherry-pick lets you apply the changes from a specific commit onto your current branch. Unlike merging (which brings all commits from a branch), cherry-pick lets you selectively choose individual commits.
How Cherry-Pick Works
Cherry-pick creates a NEW commit (E') with the same changes as E but a different SHA hash.
Basic Cherry-Pick
# Switch to the branch where you want the commit
git checkout main
# Apply a specific commit by its hash
git cherry-pick abc1234[main f7e8d9c] Add input validation for email field Date: Mon Jan 15 10:30:00 2024 1 file changed, 15 insertions(+), 2 deletions(-)
Step-by-Step Example
# View commit log of another branch to find the commit you want
git log feature/auth --onelinee5f6g7h (feature/auth) Add session timeout d4e5f6g Add password hashing c3d4e5f Add login endpoint b2c3d4e Set up auth middleware
# Cherry-pick the password hashing commit to main
git checkout main
git cherry-pick d4e5f6g[main a1b2c3d] Add password hashing Author: Jane Doe <jane@example.com> Date: Tue Jan 16 14:00:00 2024 1 file changed, 23 insertions(+), 5 deletions(-)
Cherry-Pick Multiple Commits
Individual Commits
git cherry-pick abc1234 def5678 ghi9012Range of Commits
# Cherry-pick commits from B to E (B is exclusive, E is inclusive)
git cherry-pick B..E
# Include B as well
git cherry-pick B^..EExample with Range
# Apply commits c3d4e5f through e5f6g7h
git cherry-pick c3d4e5f^..e5f6g7h[main 111aaaa] Add login endpoint [main 222bbbb] Add password hashing [main 333cccc] Add session timeout
Cherry-Pick Options
Apply Without Committing
git cherry-pick --no-commit abc1234
# or
git cherry-pick -n abc1234Changes are staged but not committed. Useful for combining multiple cherry-picks into one commit:
git cherry-pick -n commit1
git cherry-pick -n commit2
git cherry-pick -n commit3
git commit -m "Backport: security fixes from feature branch"Edit Commit Message
git cherry-pick -e abc1234
# Opens editor to modify the commit messageAppend Cherry-Pick Info to Message
git cherry-pick -x abc1234Adds a line like (cherry picked from commit abc1234...) to the commit message.
Keep Original Author
By default, cherry-pick preserves the original author. The committer changes to you:
git log --format="%an (author) | %cn (committer)" -1Jane Doe (author) | You (committer)
Handling Cherry-Pick Conflicts
git cherry-pick abc1234Auto-merging src/app.js CONFLICT (content): Merge conflict in src/app.js error: could not apply abc1234... Add feature hint: After resolving the conflicts, mark the corrected paths hint: with 'git add <paths>' or 'git rm <paths>' hint: and commit the result with 'git cherry-pick --continue'
Resolve and Continue
# Fix conflicts in the file
vim src/app.js
# Stage resolved file
git add src/app.js
# Continue cherry-pick
git cherry-pick --continueAbort Cherry-Pick
git cherry-pick --abortSkip Current Commit
git cherry-pick --skipCommon Use Cases
Backporting a Bug Fix
# Fix was made on develop, need it on release branch
git checkout release/v2.0
git cherry-pick abc1234 # The bug fix commit from developHotfix to Production
# Apply critical fix to main without merging entire feature branch
git checkout main
git cherry-pick fix-commit-hash
git push origin main
# Deploy immediatelyRecovering Work from a Deleted Branch
# Find the commit hash from reflog
git reflog | grep "important work"
# Cherry-pick it to current branch
git cherry-pick recovered-commit-hashSelective Feature Deployment
# Only deploy parts of a feature that are ready
git checkout deploy
git cherry-pick feature-part1-commit
git cherry-pick feature-part3-commit
# Skip part 2 which is not readyCherry-Pick vs Other Approaches
| Approach | Use Case | Creates New Commits |
|---|---|---|
| Cherry-pick | Specific commits to another branch | Yes (new SHAs) |
| Merge | All commits from a branch | Yes (merge commit) |
| Rebase | Move entire branch to new base | Yes (new SHAs) |
| Patch | Share changes without Git connection | Via git apply |
Best Practices
- Use
-xflag to track where cherry-picks came from - Avoid cherry-picking merge commits — they are complex and error-prone
- Cherry-pick is not a substitute for merging — use it sparingly
- Watch for duplicate changes — cherry-picked commits will conflict when branches merge later
- Prefer merging or rebasing when you need all changes from a branch
Interview Questions
- What is git cherry-pick and when would you use it?
Cherry-pick applies the changes from a specific commit to your current branch, creating a new commit. Use it for backporting fixes, applying hotfixes to production, or selectively applying changes without merging an entire branch.
- Does cherry-pick change the original commit?
No, the original commit remains unchanged on its branch. Cherry-pick creates a new commit with the same changes but a different SHA hash on the target branch.
- What problems can cherry-picking cause during later merges?
When the original branch is eventually merged, Git may detect duplicate changes and create conflicts. The same logical change exists in two commits with different SHAs, which can confuse merge algorithms.
- How do you cherry-pick a merge commit?
Use git cherry-pick -m 1 <merge-commit> where -m 1 specifies which parent to use as the mainline. However, this is complex and generally discouraged.
- How do you cherry-pick multiple commits without creating separate commits?
Use git cherry-pick --no-commit commit1 commit2 commit3 then git commit once. This stages all changes without committing, letting you create a single combined commit.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Cherry-Pick - Apply Specific Commits to Any Branch.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, advanced, cherry, pick, git cherry-pick - apply specific commits to any branch
Related Git & GitHub Topics