Lesson Completion
Back to course

Stash Best Practices

Beginner
8 minutes4.8Git

The Hook (The "Byte-Sized" Intro)

Stash is a parking lot, not a garage. You park your car for an hour while you grab coffee — you don't leave it there for three months. Developers who treat stash as long-term storage end up with 20 mystery entries of forgotten, outdated code. Use stash for minutes to hours, then either commit or discard. Here are the rules.

📖 What are Stash Best Practices?

These are habits that keep stashing safe, efficient, and confusion-free. They prevent data loss, keep your stash list clean, and help you decide when stashing is the right tool versus alternatives.

Conceptual Clarity

The 6 rules of stashing:

#RuleWhy
1Always add a messagegit stash -m "msg" — unlabeled stashes become mystery boxes
2Keep stashes short-livedHours, not days. Commit or discard within the session
3Use apply before dropVerify changes work before deleting the stash
4Prefer branches for longer workIf it'll live > 1 day, create a WIP branch instead
5Clean up regularlyDrop applied stashes, review old ones weekly
6Include untracked when neededUse -u when you need a truly clean workspace

Real-Life Analogy

Stashing best practices are like rules for a hotel safe:

  • Label what's inside (message)
  • Don't leave things there for months (keep short-lived)
  • Check the contents before checking out (apply before drop)
  • For long-term storage, use a bank vault instead (branches)

Visual Architecture

flowchart TD NEED["Need to set work aside?"] --> DURATION{"How long?"} DURATION -->|"Minutes to hours"| STASH["📦 git stash -m 'msg'"] DURATION -->|"Days to weeks"| BRANCH["🌿 git switch -c wip/task"] STASH --> RETURN["Return quickly"] RETURN --> POP["git stash pop"] BRANCH --> COMMIT["Regular commits"] style STASH fill:#0f3460,stroke:#ffd700,color:#ffd700 style BRANCH fill:#1a1a2e,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • Prevents data loss: apply + verify + drop is safer than pop.
  • Reduces confusion: Messages and short lifespans prevent stash graveyard.
  • Right tool for the job: Stash for quick context switches; branches for sustained work.
  • Team hygiene: Clean stash habits reflect professional workflow discipline.

Code

bash
# ─── GOOD: labeled, short-lived stash ─── git stash -m "WIP: fix sidebar alignment" # ... handle the interruption ... git stash pop # Done in 20 minutes ✅ # ─── BAD: unlabeled stash left for days ─── git stash # 3 days later: git stash list # stash@{0}: WIP on main: a1b2c3d ... (what was this??) # ─── When stash is NOT the right tool ─── # If work will take more than a day, use a branch: git switch -c wip/payment-redesign git add . git commit -m "WIP: payment redesign in progress" # Come back anytime, no mystery, no expiry # ─── Safe pop workflow ─── git stash apply # Apply (keep the stash) # Verify everything works... git stash drop stash@{0} # Now safe to remove # ─── Weekly cleanup ─── git stash list # Review all stashes git stash show -p stash@{3} # Inspect old ones git stash drop stash@{3} # Drop what's obsolete

Stash vs Branch Decision

ScenarioUse StashUse Branch
Quick context switch (minutes)❌ Overkill
Interrupted mid-task (hours)✅ Either works
Work paused for days
Experimental code to save
Sharing work with others✅ (push branch)

Key Takeaways

  • Always label stashes with -m — your future self will thank you.
  • Keep stashes short-lived — for quick context switches, not long-term storage.
  • Use branches for work that will last more than a day.
  • Apply before drop — verify first, delete second.

Interview Prep

  • Q: When should you use a branch instead of a stash? A: When the work will last more than a day, when you want to share progress with teammates, or when you need regular commits. Stashes are for short interruptions; branches are for sustained work.

  • Q: What is the safest workflow for restoring a stash? A: Use git stash apply (keeps the stash), verify the changes work correctly, then git stash drop to remove it. This prevents data loss if the apply creates conflicts or unexpected issues.

  • Q: How do you prevent stash clutter? A: Always add messages with -m, drop stashes as soon as they're applied, review and clean up the stash list weekly, and prefer branches for anything longer than a few hours.

Topics Covered

Git StashingGit Fundamentals

Tags

#git#stash#best-practices#workflow

Last Updated

2026-02-12