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:

Bash

You realize the XSS fix on main (commit 89abcde) is urgently needed on your release/v1.0 branch.

Step-by-step:

  1. Switch to the target branch
Bash
  1. Cherry-pick the good commit
Bash

Possible outcomes:

Clean cherry-pick (no conflict)

text

→ New commit created with same changes, same message, same author/date (unless overridden)

Conflict happens (very common)

text

Fix conflict like any merge:

  • Open file → resolve markers
  • git add comment.js
  • git cherry-pick –continue

Or abandon:

Bash
  1. Push the fixed branch
Bash

→ 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:

Bash

She emails / uploads the .patch file to Bob.

Bob receives patch & applies it:

Bash

Output:

text

→ New commit created with:

  • same changes
  • same message
  • same author (Alice)
  • same date

Bob can now push:

Bash

→ Alice’s fix is now in the repo with full attribution.

git apply (without commit)

Bash

→ 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! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *