Lesson 1: The Cherry Picker (Stash & Cherry-Pick)
Sometimes you're in the middle of cooking dinner when the fire alarm goes off. You need to save your half-cooked meal, handle the emergency, and come back to finish cooking. That's what git stash and git cherry-pick are for.
git stash — The Emergency Save
You're working on a feature when an urgent bug report comes in. You can't commit half-finished code, but you can't lose it either. Stash saves your changes in a temporary pocket.
git stash # Save current changes
git stash save "WIP login" # Save with a description
After stashing, your working directory is clean — as if you never made those changes. Now you can switch branches, fix the bug, and come back.
Managing Your Stash
git stash list # See all stashed changes
stash@{0}: WIP on feature: a1b2c3d WIP login
stash@{1}: WIP on main: e4f5g6h Fix typo
git stash pop # Restore most recent stash & remove it
git stash apply # Restore most recent stash & keep it
git stash drop stash@{1} # Delete a specific stash
git stash clear # Delete ALL stashes
git cherry-pick — The Surgical Transplant
What if you want just one specific commit from another branch, without merging the entire branch?
git cherry-pick abc123
This copies commit abc123 and applies it to your current branch as a new commit.
When to Cherry-Pick
| Scenario | Example |
|----------|---------|
| Hotfix backport | Apply a fix from main to release/v1.0 |
| Selective feature | Grab one commit from a feature branch |
| Undo a revert | Cherry-pick the original commit back |
Cherry-Pick Workflow
# 1. Find the commit hash you want
git log --oneline feature/payments
# f6e5d4c Add payment validation
# a1b2c3d Add payment form
# 2. Switch to your target branch
git checkout main
# 3. Cherry-pick the specific commit
git cherry-pick f6e5d4c
# 4. The commit is now on main!
git log --oneline
# x9y8z7w Add payment validation (cherry-picked)
Mission Objective
Practice emergency operations:
- Emergency save: Run
git stashto pocket your current changes. - Check the pocket: Run
git stash listto see what's stashed. - Restore: Run
git stash popto bring your changes back.