Lesson Completion
Back to course

Cherry-pick for Recovery

Beginner
8 minutes4.7Git

The Hook (The "Byte-Sized" Intro)

A teammate accidentally committed a critical bugfix on the wrong branch. The whole branch is messy and can't be merged. But that one commit? It's perfect. git cherry-pick copies exactly that one commit onto your current branch — no merge, no mess, just the fix. It's a surgical strike for commit-level precision.

📖 What is Cherry-pick for Recovery?

git cherry-pick takes a specific commit by its SHA and replays it on your current branch, creating a new commit with the same changes but a new hash. It's used for targeted recovery, hotfixes, and selective backporting.

Conceptual Clarity

What cherry-pick does:

Before: main: A ─ B ─ C feature: D ─ E ─ F (you want only E) After cherry-pick E onto main: main: A ─ B ─ C ─ E' feature: D ─ E ─ F (unchanged)

Common use cases:

ScenarioHow Cherry-pick Helps
Bugfix on wrong branchCopy the fix commit to the right branch
Selective backportApply one feature commit to a release branch
Recovery after resetRescue a specific commit from reflog
Partial mergeNeed 1 commit from a branch, not all of them

Real-Life Analogy

Cherry-pick is like photocopying one page from a book and pasting it into a different notebook. The original book stays the same, and your notebook gets exactly the content you need.

Visual Architecture

flowchart LR SOURCE["🌿 Source Branch<br/>Commit E"] -->|"git cherry-pick E"| TARGET["🌿 Target Branch<br/>+ Commit E'"] style SOURCE fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style TARGET fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • Precision: Copy exactly one commit without merging an entire branch.
  • Recovery: Rescue individual commits from deleted branches or reflog.
  • Hotfixes: Apply a fix from develop to a release branch without a full merge.
  • Safety: Non-destructive — the source branch is unchanged.

Code

bash
# ─── Cherry-pick a single commit ─── git switch main git cherry-pick abc1234 # Creates a new commit E' with the same changes as abc1234 # ─── Cherry-pick without auto-committing ─── git cherry-pick abc1234 --no-commit # Changes applied to working tree, not committed yet # Useful when you want to modify before committing # ─── Cherry-pick multiple commits ─── git cherry-pick abc1234 def5678 ghi9012 # ─── Cherry-pick a range ─── git cherry-pick abc1234..ghi9012 # Applies commits AFTER abc1234 up to ghi9012 # ─── If cherry-pick conflicts ─── # Resolve the conflict, then: git add . git cherry-pick --continue # ─── Abort a cherry-pick ─── git cherry-pick --abort # ─── Recovery: cherry-pick from reflog ─── git reflog # Find: abc1234 HEAD@{5}: commit: Important fix git cherry-pick abc1234

Key Takeaways

  • Cherry-pick copies one commit to your current branch — creating a new commit with a new hash.
  • Use it for targeted fixes, selective recovery, and backporting.
  • The source branch is never modified.
  • Conflicts are possible and resolved the same way as merge conflicts.

Interview Prep

  • Q: What does git cherry-pick do? A: It takes a specific commit by SHA and replays its changes on the current branch, creating a new commit with the same diff but a new hash and parent. The source branch remains unchanged.

  • Q: When would you use cherry-pick instead of merge? A: When you need only one or a few specific commits from another branch, not the entire branch history. Common scenarios include applying a hotfix from develop to a release branch, or rescuing a good commit from an otherwise messy branch.

  • Q: Can cherry-pick cause conflicts? A: Yes. If the current branch has changes in the same lines that the cherry-picked commit modifies, Git will report a conflict. Resolve it the same way as a merge conflict — edit the files, git add, and git cherry-pick --continue.

Topics Covered

Git RecoveryGit Cherry-pick

Tags

#git#cherry-pick#recovery#beginner-friendly

Last Updated

2026-02-13