Lesson Completion
Back to course

Stashing Untracked Files

Beginner
7 minutes4.7Git

The Hook (The "Byte-Sized" Intro)

You stash your work, try to switch branches, and Git still complains about new files cluttering the workspace. That's because git stash ignores untracked files by default — it only saves modifications to files Git already knows about. Add -u and suddenly untracked files get swept into the stash too. Add -a and even ignored files join the party. Clean workspace, guaranteed.

📖 What is Stashing Untracked Files?

By default, git stash only saves changes to tracked files. To include brand-new files that haven't been added to Git, you need the -u flag. To include ignored files too (like build artifacts), use -a.

Conceptual Clarity

FlagWhat Gets StashedUse Case
(none)Tracked modified + stagedQuick context switch
-u / --include-untracked+ new untracked filesNeed a fully clean workspace
-a / --all+ untracked + ignored filesNuclear clean (rare)

Why default stash skips untracked files: Git is conservative — it doesn't want to stash files you might not want back. New files could be build artifacts, temp files, or editor configs. The default protects you from accidentally stashing junk.

Real-Life Analogy

  • Default stash = Cleaning only what's on your desk (organized items)
  • -u stash = Also cleaning loose papers scattered around (new documents)
  • -a stash = Also emptying the trash can next to the desk (ignored files)

Visual Architecture

flowchart TD TRACKED["📄 Tracked Changes"] -->|"git stash"| STASH["📦 Stash"] UNTRACKED["📄 Untracked Files"] -->|"git stash -u"| STASH IGNORED["📄 Ignored Files"] -->|"git stash -a"| STASH style TRACKED fill:#1a1a2e,stroke:#53d8fb,color:#53d8fb style UNTRACKED fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style IGNORED fill:#1a1a2e,stroke:#e94560,color:#e94560 style STASH fill:#0f3460,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • Branch switching: Some workflows require a completely clean working tree.
  • Build cleanup: Stash or clean build artifacts when they interfere with operations.
  • New file safety: Don't lose newly created files when context switching.
  • CI reproducibility: Start from a pristine state.

Code

bash
# ─── Default stash misses untracked files ─── echo "new feature" > new-feature.js git stash git status # Output: new-feature.js still showing as untracked! # ─── Include untracked files ─── git stash -u -m "WIP: including new files" git status # Output: nothing to commit, working tree clean ✅ # ─── Include everything (even ignored) ─── git stash -a -m "Full clean for debugging" # ─── Restore with untracked files ─── git stash pop # new-feature.js is restored! # ─── Verify what's in the stash ─── git stash show --include-untracked

Key Takeaways

  • Default git stash only saves tracked file changes — new files stay behind.
  • Use -u to include untracked files for a fully clean workspace.
  • Use -a to include ignored files too (rarely needed).
  • Untracked files are restored when you apply or pop the stash.

Interview Prep

  • Q: Why doesn't git stash include untracked files by default? A: Git is conservative — untracked files could be build artifacts or temp files that you don't want in the stash. The default protects against accidentally stashing and restoring junk files.

  • Q: How do you stash absolutely everything for a clean workspace? A: Use git stash -a (or --all). This stashes tracked modifications, staged changes, untracked files, AND ignored files. It's the most thorough option but rarely needed.

  • Q: What happens to untracked files in a stash when you pop it? A: The untracked files are recreated in the working tree exactly as they were when stashed. If a file with the same name already exists, Git will report a conflict.

Topics Covered

Git StashingGit Fundamentals

Tags

#git#stash#untracked#beginner-friendly

Last Updated

2026-02-12