The Hook (The "Byte-Sized" Intro)
A stash without a message is like a box with no label — fine when you have one, useless when you have five. git stash -m "WIP: payment API integration" takes 2 extra seconds and saves you 5 minutes of confusion later. Here's every way to create a stash, from basic to surgical.
📖 What is Creating Stashes?
Creating a stash means saving your current uncommitted changes into Git's stash stack. You can stash everything, stash selectively, add messages, and include or exclude untracked files.
Conceptual Clarity
Stash creation options:
| Command | What It Stashes |
|---|---|
git stash | Tracked modified + staged changes |
git stash -m "msg" | Same + adds a descriptive label |
git stash -u | + untracked files |
git stash -a | + untracked + ignored files |
git stash push <file> | Only specific files |
git stash -p | Interactive — choose hunks to stash |
git stash --keep-index | Stash unstaged only, keep staged intact |
Real-Life Analogy
Creating a stash is like packing items into a labeled storage box:
git stash= Pack everything on the deskgit stash -m= Pack and label the boxgit stash push file.js= Pack only specific itemsgit stash -p= Sort through and pack items one by one
Visual Architecture
Why It Matters
- Organization: Messages make stashes identifiable weeks later.
- Precision: Partial stashing lets you save only what you need to set aside.
- Workflow control:
--keep-indexlets you stash unstaged work while keeping staged changes for testing. - Clean switches: Including untracked files (
-u) ensures a fully clean workspace.
Code
# ─── Basic stash ───
git stash
# Stashes tracked + staged changes
# ─── Stash with a message (ALWAYS do this!) ───
git stash -m "WIP: payment form validation"
# ─── Include untracked files ───
git stash -u -m "WIP: new checkout page"
# ─── Stash only specific files ───
git stash push src/login.js src/auth.js -m "WIP: auth refactor"
# Only these two files are stashed; everything else stays
# ─── Stash interactively (choose hunks) ───
git stash -p
# Git shows each changed hunk and asks:
# Stash this hunk [y,n,q,a,d,/,s,e,?]?
# y = stash this hunk
# n = skip this hunk
# s = split into smaller hunks
# ─── Keep staged changes, stash only unstaged ───
git stash --keep-index -m "Stash unstaged only"
# Staged changes remain in the staging area
# Useful for testing what you've staged
# ─── Verify the stash was created ───
git stash list
# Output:
# stash@{0}: On main: WIP: payment form validation
# stash@{1}: On feature/login: WIP: auth refactorKey Takeaways
- Always use
-mto label your stashes — unlabeled stashes become mystery boxes. - Use
-uto include untracked files for a completely clean workspace. - Use
git stash push <files>to stash only specific files. - Use
git stash -pfor surgical, hunk-by-hunk stashing.
Interview Prep
-
Q: How do you stash only specific files? A: Use
git stash push <file1> <file2>. This stashes only the specified files while leaving everything else in your working tree. -
Q: What does
git stash --keep-indexdo? A: It stashes only the unstaged changes, keeping the staging area intact. This is useful when you want to test exactly what you've staged while temporarily putting aside other modifications. -
Q: How do you stash changes interactively? A: Use
git stash -p(patch mode). Git presents each change hunk and asks whether to stash it. You can accept, skip, or split hunks — giving you fine-grained control over what gets stashed.