How do you cherry-pick a commit in Git?
Copy a specific commit from one branch to another without merging the whole branch using git cherry-pick.
GitVersion Control
Overview
`git cherry-pick <commit>` takes a single commit from any branch and applies it to your current branch as a new commit. This is useful when you want to backport a bug fix to an older release branch or pull in just one change without merging everything else.
Key Points
- •`git cherry-pick <hash>` applies one commit to the current branch.
- •A new commit is created with the same changes but a different hash.
- •Cherry-pick multiple commits by listing them: `git cherry-pick A B C`.
- •Cherry-pick a range: `git cherry-pick A^..B` (includes A through B).
- •Conflicts are possible resolve them the same way as a merge conflict, then `git cherry-pick --continue`.
Examples
Cherry-pick a single commit onto the current branch
bash# Find the commit hash
git log --oneline feature/payment
# Apply it to your current branch (e.g. main)
git cherry-pick a1b2c3dCreates a new commit on the current branch with the same changes.
Cherry-pick multiple specific commits
bashgit cherry-pick a1b2c3d e4f5g6hEach commit is applied in order as a separate new commit.
Cherry-pick a range of commits
bash# All commits from A (exclusive) to B (inclusive)
git cherry-pick A..B
# All commits from A (inclusive) to B (inclusive)
git cherry-pick A^..BHandle a conflict during cherry-pick
bash# 1. Git stops with a conflict
git cherry-pick a1b2c3d
# 2. Resolve the conflict in the file(s)
# 3. Stage the resolved files
git add src/fixed-file.ts
# 4. Continue
git cherry-pick --continue
# Or abort entirely
git cherry-pick --abortPro Tip
Cherry-picking is great for backporting fixes to release branches. For example: fix a bug on `main`, then cherry-pick the fix commit onto `release/v2`. Avoid cherry-picking large feature work - a full merge is cleaner in that case.