C.W.K.
Stream
Lesson 04 of 05 · published

Cherry-Pick and rerere

~22 min · cherry-pick, rerere

Level 0Untracked Rookie
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Cherry-pick borrows; rerere remembers

Sometimes you want one commit from another branch without merging the whole branch. git cherry-pick <hash> takes the changes introduced by that commit and applies them on top of your current branch as a new commit. The new commit has a different hash from the source — it is a copy, not a move. Use cases: backporting a bug fix from main to a release branch, lifting a single useful commit out of an experiment branch you are otherwise abandoning, propagating a fix across long-lived parallel branches.

Cherry-pick can take a range — git cherry-pick A..B applies every commit after A through B in order. Conflicts pause the operation like a merge or rebase; resolve, git add, git cherry-pick --continue. --abort rewinds. -x appends "(cherry picked from commit <hash>)" to the message — invaluable when you cherry-pick across branches and want the audit trail to point at the source.

git rerere ("reuse recorded resolution") is the unsung hero of repos with long-running parallel branches. Once enabled, every conflict you resolve is recorded by Git. The next time the same conflict appears — same hunk, same content on both sides — Git resolves it automatically using your recorded answer. The labor savings on a maintenance branch that periodically merges with main are dramatic. Enable globally with git config --global rerere.enabled true and forget about it; the savings accumulate silently.

The combination shines in maintenance work. Suppose you backport ten fixes from main to a release branch. Without rerere, every cherry-pick that touches lines main has since refactored produces a fresh manual conflict. With rerere on, you resolve each pattern once; subsequent cherry-picks involving the same conflict pattern auto-resolve. git rerere status shows what is recorded. git rerere clear wipes the cache when stored resolutions become wrong.

Code

Cherry-pick basics·bash
# Pick one commit from another branch:
git cherry-pick abc1234

# Pick a range (A exclusive, B inclusive):
git cherry-pick A_HASH..B_HASH

# Pick with attribution in the message:
git cherry-pick -x abc1234

# Skip the auto-commit (stage only):
git cherry-pick -n abc1234

# Resume after resolving conflicts:
git add resolved-file
git cherry-pick --continue

# Bail out:
git cherry-pick --abort
Enable rerere and observe·bash
# Turn it on globally (or per-repo):
git config --global rerere.enabled true

# After a conflict, resolve, commit. Git records the resolution silently.

# See what's recorded:
git rerere status
git rerere diff

# When it auto-resolves a future conflict, you'll see in conflict output:
#   Resolved 'src/auth.js' using previous resolution.

# Wipe the cache if a recorded resolution is wrong:
git rerere clear

External links

Exercise

Set up two branches, main and release-1.0. Land a small fix on main as a single commit. git switch release-1.0, then git cherry-pick -x <fix-hash>. Verify the cherry-picked commit on release-1.0 has the attribution line. Separately, enable rerere globally, engineer a conflict, resolve it, then deliberately re-create the same conflict on a different branch. Confirm rerere auto-resolves the second occurrence.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.