Chapter 47: Git Cherry-pick & Patch
Git cherry-pick and its close cousin git apply / git am (working with patches)
These two features are not everyday commands like commit / push / pull / rebase — they are precision instruments. They let you say:
“I only want this one specific commit (or this one specific change) from somewhere else — bring it here, now, without touching anything else.”
Very few Git features give you that level of control. Once you master cherry-pick and patch workflows, you feel like you have surgical scissors for Git history.
1. git cherry-pick — The Most Common Advanced Use Case
git cherry-pick <commit-hash> = “copy exactly the changes from one commit and apply them as a brand-new commit on my current branch”
Important points:
- The new commit gets a new hash (different from original)
- But it contains exactly the same diff (same lines added/removed)
- Author / date can be kept or overridden
- You can cherry-pick one commit, several commits, or even a range
Real-life situations where cherry-pick shines:
- A bugfix was made on develop → you need the same fix on release/v1.2 without merging the whole develop branch
- You find one perfect commit in a messy/abandoned branch
- You want to backport a security patch from main to an older release branch
- You accidentally committed on wrong branch → cherry-pick it to the right one and drop the wrong commit
Example — Realistic cherry-pick workflow
You have two branches:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
git log main --oneline -5 # 89abcde (main) Fix critical XSS in comment form # fedcba9 Update dependencies # 456def7 Add user profile page ... git log feature/old-experiment --oneline -5 # 123abcd Add dark mode toggle (but incomplete) # ... |
You realize the XSS fix on main (commit 89abcde) is urgently needed on your release/v1.0 branch.
Step-by-step:
- Switch to the target branch
|
0 1 2 3 4 5 6 7 |
git switch release/v1.0 git pull origin release/v1.0 |
- Cherry-pick the good commit
|
0 1 2 3 4 5 6 |
git cherry-pick 89abcde |
Possible outcomes:
Clean cherry-pick (no conflict)
|
0 1 2 3 4 5 6 7 8 |
[release/v1.0 new-hash] Fix critical XSS in comment form Date: Fri Feb 13 19:20:00 2026 +0530 3 files changed, 15 insertions(+), 5 deletions(-) |
→ New commit created with same changes, same message, same author/date (unless overridden)
Conflict happens (very common)
|
0 1 2 3 4 5 6 7 8 9 |
Auto-merging comment.js CONFLICT (content): Merge conflict in comment.js error: could not apply 89abcde... Fix critical XSS in comment form hint: Resolve all conflicts, then run "git cherry-pick --continue" |
Fix conflict like any merge:
- Open file → resolve markers
- git add comment.js
- git cherry-pick –continue
Or abandon:
|
0 1 2 3 4 5 6 |
git cherry-pick --abort |
- Push the fixed branch
|
0 1 2 3 4 5 6 |
git push origin release/v1.0 |
→ The fix is now on the release branch — no unnecessary merge of the whole main.
Useful cherry-pick flags (you will use these)
| Flag / Option | Example | What it does |
|---|---|---|
| -e / –edit | git cherry-pick -e abc1234 | Edit commit message before applying |
| -n / –no-commit | git cherry-pick -n abc1234 | Apply changes but do not commit — good for combining |
| –ff | git cherry-pick –ff abc1234 | Fast-forward if possible (rarely needed) |
| Multiple commits | git cherry-pick abc1234 def5678 | Apply several in order |
| Range | git cherry-pick bad1^..good3 | Apply all commits after bad1 up to good3 |
2. git apply & git am — Working with patch files
Sometimes you don’t have direct access to the source repository (or you want to apply changes offline).
A patch file = plain-text file containing diff (changes) that git apply or git am can read.
Two main commands:
| Command | Full name | What it does | Creates commit? | Most used when? |
|---|---|---|---|---|
| git apply | git apply | Applies the diff to working directory & index | No | Quick test / manual patch application |
| git am | git am (“apply mailbox”) | Applies patch + creates commit with original author/message | Yes | Applying emailed patches or Git-format-patch files |
Real example — git format-patch + git am workflow
Alice made a fix on her laptop but cannot push directly (maybe offline / firewall / no write access).
Alice creates patch file:
|
0 1 2 3 4 5 6 7 8 |
# on Alice's machine git format-patch -1 HEAD # creates file: 0001-Fix-XSS-in-comment-form.patch |
She emails / uploads the .patch file to Bob.
Bob receives patch & applies it:
|
0 1 2 3 4 5 6 7 |
# Bob downloads patch to current folder git am 0001-Fix-XSS-in-comment-form.patch |
Output:
|
0 1 2 3 4 5 6 |
Applying: Fix XSS in comment form |
→ New commit created with:
- same changes
- same message
- same author (Alice)
- same date
Bob can now push:
|
0 1 2 3 4 5 6 |
git push origin main |
→ Alice’s fix is now in the repo with full attribution.
git apply (without commit)
|
0 1 2 3 4 5 6 |
git apply 0001-Fix-XSS-in-comment-form.patch |
→ Changes appear in working directory & staged — Bob can review / edit before committing.
3. Quick cherry-pick & patch Cheat Sheet (2026)
| Task | Command / Pattern | When / Why |
|---|---|---|
| Copy one commit to current branch | git cherry-pick abc1234 | Backport fix / steal one good commit |
| Copy + edit message | git cherry-pick -e abc1234 | Change author/message/date |
| Apply without committing | git cherry-pick -n abc1234 | Want to combine with other changes |
| Create patch from one commit | git format-patch -1 HEAD | Send patch via email / offline |
| Apply patch + create commit | git am patch-file.patch | Receive emailed patch & keep author/message |
| Apply patch without commit | git apply patch-file.patch | Review / edit before committing |
Got the cherry-pick & patch feeling now?
git cherry-pick = “transplant one specific commit here, now” git format-patch / git am = “send & receive commit(s) as portable patch files”
Both are precision tools — used when you want one piece of history, not the whole branch.
Next?
- Want a full “cherry-pick a fix from develop to release branch” demo?
- See how to use git format-patch + git am in team workflow?
- Or wrap up with one final big summary of all Git concepts we covered?
Just tell me — we’ll finish strong. You’ve now mastered Git from zero to very advanced — truly impressive! 🚀
