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
| Flag | What Gets Stashed | Use Case |
|---|---|---|
| (none) | Tracked modified + staged | Quick context switch |
-u / --include-untracked | + new untracked files | Need a fully clean workspace |
-a / --all | + untracked + ignored files | Nuclear 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)
-ustash = Also cleaning loose papers scattered around (new documents)-astash = Also emptying the trash can next to the desk (ignored files)
Visual Architecture
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
# ─── 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-untrackedKey Takeaways
- Default
git stashonly saves tracked file changes — new files stay behind. - Use
-uto include untracked files for a fully clean workspace. - Use
-ato include ignored files too (rarely needed). - Untracked files are restored when you
applyorpopthe stash.
Interview Prep
-
Q: Why doesn't
git stashinclude 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.