Lesson Completion
Back to course

What is Stashing?

Beginner
9 minutes4.8Git

The Hook (The "Byte-Sized" Intro)

You're halfway through a feature when your lead says "drop everything — there's a production bug." You can't commit half-finished code. You can't switch branches with a dirty working tree. git stash is the panic button: it saves all your uncommitted changes, gives you a clean workspace, and lets you come back later as if nothing happened. It's Git's version of a bookmark.

📖 What is Stashing?

git stash temporarily shelves (or "stashes") your uncommitted changes — both staged and unstaged — so you can work on something else. When you're ready, you can re-apply the stashed changes. The stash is stored as a stack: last in, first out.

Conceptual Clarity

What gets stashed by default:

TypeStashed?Flag to Include
Tracked modified files✅ Yes(default)
Staged changes✅ Yes(default)
Untracked files❌ No-u or --include-untracked
Ignored files❌ No-a or --all

How the stash works internally:

  • Stashes are stored as special commits in .git/refs/stash
  • They form a stack — stash@{0} is the most recent, stash@{1} the next, etc.
  • Stashes persist across branch switches, reboots, and terminal closures

Real-Life Analogy

Stashing is like putting your half-assembled LEGO project in a labeled box on a shelf. Your desk is now clear to work on something else. When you come back, you open the box and continue exactly where you left off. The box is labeled so you remember what's inside.

Visual Architecture

flowchart LR WT["💻 Working Tree<br/>(dirty)"] -->|"git stash"| STASH["📦 Stash Stack"] STASH -->|"git stash pop"| WT2["💻 Working Tree<br/>(restored)"] WT -->|"After stash"| CLEAN["✨ Clean Workspace"] style WT fill:#1a1a2e,stroke:#e94560,color:#e94560 style STASH fill:#0f3460,stroke:#ffd700,color:#ffd700 style WT2 fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb style CLEAN fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • Context switching: Move between tasks without losing work or making messy commits.
  • Clean workspace: Some operations (like git pull or git switch) require a clean tree.
  • Temporary: Stash is designed for minutes or hours, not weeks of storage.
  • Non-destructive: Your changes are saved safely and can be reapplied anytime.

Code

bash
# ─── Basic stash (save everything tracked) ─── git stash # Output: Saved working directory and index state WIP on main: a1b2c3d # ─── Stash with a descriptive message ─── git stash -m "WIP: login form validation" # Much easier to identify later! # ─── Stash including untracked files ─── git stash -u # Also stashes new files that haven't been git-added yet # ─── Stash everything (including ignored files) ─── git stash -a # ─── Verify your workspace is clean ─── git status # Output: nothing to commit, working tree clean # ─── Now you can safely switch branches ─── git switch hotfix/prod-bug # Fix the bug, commit, push, switch back git switch feature/login git stash pop # Your login form changes are back!

Key Takeaways

  • git stash saves uncommitted changes (tracked + staged) and gives you a clean workspace.
  • Use -m "message" to label your stashes — you'll thank yourself later.
  • Use -u to include untracked files.
  • Stash is a stack — most recent stash is stash@{0}.

Interview Prep

  • Q: What is git stash and when would you use it? A: git stash temporarily saves uncommitted changes so you can switch to a different task with a clean working tree. Common scenario: you're mid-feature and need to switch branches to fix a production bug.

  • Q: Does git stash save untracked files by default? A: No. By default it only saves tracked modifications and staged changes. Use git stash -u (or --include-untracked) to also stash untracked files.

  • Q: Where are stashes stored? A: Stashes are stored as special commit objects in .git/refs/stash. They form a stack indexed from stash@{0} (most recent) upward, and persist across branch switches and terminal sessions.

Topics Covered

Git StashingGit Fundamentals

Tags

#git#stash#workflow#beginner-friendly

Last Updated

2026-02-12