By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(For engineers who need to ship code, not just pass exams)
Cherry-picking lets you grab a single commit from one branch and apply it to another. Stashing lets you temporarily shelve uncommitted changes so you can switch branches without losing work.
main
Real-world scenario:
You’re a DevOps engineer maintaining a legacy monorepo. A security patch (commit abc123) lands in main, but your team’s feature/new-auth branch is weeks from merging. Cherry-pick the patch to avoid rebasing 50+ commits. Meanwhile, you’re also debugging a flaky test—stash your local changes, switch to main, verify the fix, then pop your work back.
abc123
feature/new-auth
What breaks if you ignore this?- Cherry-picking: You merge entire branches just to get one commit, risking conflicts and bloating history.- Stashing: You commit half-finished work to "save it," cluttering history with WIP commits (or worse, lose changes when switching branches).
git cherry-pick <commit>
git cherry-pick A..B
A
B
--no-commit
--abort
git stash push -m "message"
git stash list
stash@{0}: On branch: message
git stash pop
pop
apply
git stash apply stash@{2}
git stash drop stash@{1}
git stash -u
git stash branch <new-branch>
git status
Scenario: A critical security patch (commit abc123) lands in main. Your feature/payment branch isn’t ready to merge, but you need the fix now.
feature/payment
Check the commit you need: bash git log --oneline main Output: abc123 (HEAD -> main) Fix: SQL injection in login endpoint def456 Add payment processing ghi789 Initial commit Note abc123—this is the commit to cherry-pick.
bash git log --oneline main
abc123 (HEAD -> main) Fix: SQL injection in login endpoint def456 Add payment processing ghi789 Initial commit
Switch to your feature branch: bash git checkout feature/payment
bash git checkout feature/payment
Cherry-pick the commit: bash git cherry-pick abc123
bash git cherry-pick abc123
If conflicts: Resolve them (see Common Mistakes below), then: bash git add . # Stage resolved files git cherry-pick --continue
bash git add . # Stage resolved files git cherry-pick --continue
Verify the fix: bash git log --oneline -1 # Check the latest commit git diff main # Compare with main (should show no differences for the fix)
bash git log --oneline -1 # Check the latest commit git diff main # Compare with main (should show no differences for the fix)
Push the change (if working with a remote): bash git push origin feature/payment
bash git push origin feature/payment
Scenario: You’re midway through a feature when a P0 bug drops. Stash your changes, fix the bug, then resume work.
Check your uncommitted changes: bash git status Output: On branch feature/dashboard Changes not staged for commit: (use "git add <file>..." to update what will be committed) modified: src/components/Dashboard.js Untracked files: (use "git add <file>..." to include in what will be committed) src/utils/newHelper.js
bash git status
On branch feature/dashboard Changes not staged for commit: (use "git add <file>..." to update what will be committed) modified: src/components/Dashboard.js Untracked files: (use "git add <file>..." to include in what will be committed) src/utils/newHelper.js
Stash your changes (include untracked files): bash git stash push -u -m "WIP: Dashboard UI overhaul"
bash git stash push -u -m "WIP: Dashboard UI overhaul"
-u
newHelper.js
-m: Adds a descriptive message.
-m
Verify the stash: bash git stash list Output: stash@{0}: On feature/dashboard: WIP: Dashboard UI overhaul
bash git stash list
stash@{0}: On feature/dashboard: WIP: Dashboard UI overhaul
Switch to main and fix the bug: bash git checkout main git pull origin main # Ensure you're up to date # ... make your fix, commit it ...
bash git checkout main git pull origin main # Ensure you're up to date # ... make your fix, commit it ...
Return to your feature branch: bash git checkout feature/dashboard
bash git checkout feature/dashboard
Apply your stashed changes: bash git stash pop
bash git stash pop
git add
If no conflicts: Your changes are restored, and the stash is deleted.
Verify your work: bash git status # Should show your original changes
bash git status # Should show your original changes
bash git cherry-pick --no-commit abc123 def456 git commit -m "Backport security fixes from main"
git merge
git rebase
bash git cherry-pick abc123 -m "Backport: SQL injection fix from main"
bash git stash drop stash@{2} # Delete a specific stash git stash clear # Delete all stashes (use with caution!)
bash git stash branch temp-branch-name
--include-untracked
git-secrets
-m 1
git cherry-pick
git cherry-pick --continue
git stash push -u
git stash apply
"What happens if you cherry-pick a merge commit?"
Stash scenarios:
git stash push
"How do you reapply a stash without deleting it?"
git stash apply stash@{n}
Trick questions:
You’re working on feature/login and have uncommitted changes. A teammate asks you to review a critical bug fix in main (commit fix123). You need to: 1. Save your work.2. Check out main and review the fix.3. Return to feature/login and restore your work.
feature/login
fix123
Solution:
git stash push -u -m "WIP: Login form validation" # Save work git checkout main # Switch to main git show fix123 # Review the fix git checkout feature/login # Return to your branch git stash pop # Restore work
Why it works: - stash push -u saves all changes (including untracked files).- stash pop reapplies and deletes the stash in one step.
stash push -u
stash pop
git cherry-pick abc123 # Apply commit abc123 git cherry-pick A..B # Apply range (A's child to B) git cherry-pick --no-commit abc123 # Stage changes without committing git cherry-pick --abort # Cancel a cherry-pick ⚠️ Avoid cherry-picking merge commits (use `git rebase` instead).
git stash push -u -m "message" # Stash changes + untracked files git stash list # List all stashes git stash pop # Apply and delete latest stash git stash apply stash@{1} # Apply stash@{1} without deleting git stash drop stash@{1} # Delete stash@{1} git stash clear # Delete all stashes git stash branch temp-branch # Create branch from stash ⚠️ `git stash pop` is destructive—use `apply` if unsure.
Final Tip: Bookmark this guide. The next time you’re mid-feature and a hotfix drops, you’ll know exactly what to do. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.