Fatskills
Practice. Master. Repeat.
Study Guide: TECH **Git Cherry-Picking & Stashing: Zero-Fluff, Hands-On Guide**
Source: https://www.fatskills.com/web-development/chapter/tech-git-cherry-picking-stashing-zero-fluff-hands-on-guide

TECH **Git Cherry-Picking & Stashing: Zero-Fluff, Hands-On Guide**

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~8 min read

Git Cherry-Picking & Stashing: Zero-Fluff, Hands-On Guide

(For engineers who need to ship code, not just pass exams)


1. What This Is & Why It Matters

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.

Why This Matters in Production

  • Cherry-picking: You’re mid-sprint, and a critical bug fix lands in main—but your feature branch isn’t ready to merge. Cherry-pick just the fix to avoid a messy merge later.
  • Stashing: You’re halfway through a feature when an urgent hotfix drops. Stash your changes, fix the bug, then pop your work back—no lost progress, no broken builds.

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.


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).


2. Core Concepts & Components


Cherry-Picking

  • git cherry-pick <commit>: Applies the changes from a specific commit to your current branch.
  • Production insight: Use this to backport fixes to release branches without merging entire feature branches.
  • git cherry-pick A..B: Picks a range of commits (from A’s child to B).
  • Production insight: Avoid this for large ranges—it’s error-prone. Prefer rebasing or merging instead.
  • --no-commit: Stages changes without committing (lets you edit before finalizing).
  • Production insight: Useful for squashing cherry-picked changes into a single commit.
  • --abort: Cancels a cherry-pick mid-operation (e.g., if conflicts arise).
  • Production insight: Always know how to bail out—conflicts in production branches are stressful.

Stashing

  • git stash push -m "message": Saves uncommitted changes (staged + unstaged) to a stack.
  • Production insight: Always add a message—"WIP: OAuth2 token refresh" is better than "temp."
  • git stash list: Shows all stashes (format: stash@{0}: On branch: message).
  • Production insight: Stashes are local—if you delete your repo, they’re gone. Use branches for long-term work.
  • git stash pop: Applies the latest stash and removes it from the stack.
  • Production insight: pop is destructive—use apply if you might need the stash again.
  • git stash apply stash@{2}: Applies a specific stash (without removing it).
  • Production insight: Useful for testing stashes across branches.
  • git stash drop stash@{1}: Deletes a specific stash.
  • Production insight: Clean up old stashes—Git doesn’t auto-delete them.
  • git stash -u: Stashes untracked files too (default ignores them).
  • Production insight: Critical for new files—otherwise, they’ll stay in your working directory.
  • git stash branch <new-branch>: Creates a branch from a stash (resolves conflicts automatically).
  • Production insight: Use this if a stash conflicts with your current branch.


3. Step-by-Step Hands-On


Prerequisites

  • A Git repo (local or remote).
  • Basic Git knowledge (branches, commits, git status).
  • A terminal open in your repo.


Task 1: Cherry-Pick a Bug Fix from main to Your Feature Branch

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.


Steps

  1. 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.

  2. Switch to your feature branch:
    bash
    git checkout feature/payment

  3. Cherry-pick the commit:
    bash
    git cherry-pick abc123

  4. If no conflicts: Git applies the changes and auto-commits.
  5. If conflicts: Resolve them (see Common Mistakes below), then:
    bash
    git add . # Stage resolved files
    git cherry-pick --continue

  6. 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)

  7. Push the change (if working with a remote):
    bash
    git push origin feature/payment


Task 2: Stash Unfinished Work to Fix a Hotfix

Scenario: You’re midway through a feature when a P0 bug drops. Stash your changes, fix the bug, then resume work.


Steps

  1. 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

  2. Stash your changes (include untracked files):
    bash
    git stash push -u -m "WIP: Dashboard UI overhaul"

  3. -u: Stashes untracked files (newHelper.js).
  4. -m: Adds a descriptive message.

  5. Verify the stash:
    bash
    git stash list

    Output:
    stash@{0}: On feature/dashboard: WIP: Dashboard UI overhaul

  6. 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 ...

  7. Return to your feature branch:
    bash
    git checkout feature/dashboard

  8. Apply your stashed changes:
    bash
    git stash pop

  9. If conflicts: Resolve them, then git add the files.
  10. If no conflicts: Your changes are restored, and the stash is deleted.

  11. Verify your work:
    bash
    git status # Should show your original changes


4. ? Production-Ready Best Practices


Cherry-Picking

  • Use --no-commit for squashing: If you cherry-pick multiple commits, use --no-commit to combine them into one clean commit.
    bash git cherry-pick --no-commit abc123 def456 git commit -m "Backport security fixes from main"
  • Avoid cherry-picking merges: Merge commits (git merge) have two parents—cherry-picking them can cause chaos. Use git rebase instead.
  • Document cherry-picks: Add a note in the commit message: bash git cherry-pick abc123 -m "Backport: SQL injection fix from main"
  • Test after cherry-picking: Always verify the change works in the new branch (e.g., run tests, check logs).

Stashing

  • Stash often, but clean up: Stashes are local and temporary. Delete old stashes with: bash git stash drop stash@{2} # Delete a specific stash git stash clear # Delete all stashes (use with caution!)
  • Use branches for long-term work: If you’ll be away from a stash for >1 day, create a branch instead: bash git stash branch temp-branch-name
  • Stash untracked files: Always use -u (or --include-untracked) to avoid losing new files.
  • Avoid stashing secrets: If your changes include API keys or passwords, stash them securely (or use git-secrets).


5. ⚠️ Common Mistakes & Traps

Mistake Symptom Fix/Prevention
Cherry-picking a merge commit Git complains about "empty commit" or applies wrong changes. Use git rebase instead. If you must cherry-pick, use -m 1 to specify the mainline parent.
Forgetting to resolve conflicts git cherry-pick hangs or shows "CONFLICT". Resolve conflicts, then git add and git cherry-pick --continue.
Stashing without -u Untracked files disappear after git stash pop. Always use git stash push -u.
Popping a stash on the wrong branch Conflicts or broken code. Use git stash apply first to test, then pop if it works.
Losing stashes git stash list is empty after a pop. pop deletes the stash. Use apply if you might need it again.


6. ? Exam/Certification Focus


Typical Question Patterns

  1. Cherry-pick scenarios:
  2. "How do you apply a single commit from main to your feature branch?"
    • Answer: git cherry-pick <commit>.
  3. "What happens if you cherry-pick a merge commit?"


    • Answer: Git may apply the wrong changes. Use git rebase instead.
  4. Stash scenarios:

  5. "How do you save uncommitted changes to switch branches?"
    • Answer: git stash push.
  6. "How do you reapply a stash without deleting it?"


    • Answer: git stash apply stash@{n}.
  7. Trick questions:

  8. "What’s the difference between git stash pop and git stash apply?"
    • Answer: pop deletes the stash after applying; apply keeps it.
  9. "How do you stash untracked files?"
    • Answer: git stash push -u.

Key Trap Distinctions

  • Cherry-pick vs. rebase:
  • Cherry-pick: Applies one commit.
  • Rebase: Replays multiple commits (use for feature branches).
  • Stash pop vs. apply:
  • pop: Destructive (deletes the stash).
  • apply: Non-destructive (keeps the stash).


7. ? Hands-On Challenge


Challenge

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.

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.


8. ? Rapid-Reference Crib Sheet


Cherry-Picking

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).

Stashing

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.


9. ? Where to Go Next

  1. Git Official Docs – Cherry-Pick
  2. Git Official Docs – Stash
  3. Atlassian Git Tutorial – Cherry-Picking
  4. GitHub Skills – Stashing (Interactive tutorial)

Final Tip: Bookmark this guide. The next time you’re mid-feature and a hotfix drops, you’ll know exactly what to do. ?



ADVERTISEMENT