Lesson Completion
Back to course

Creating Stashes

Beginner
9 minutes4.8Git

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:

CommandWhat It Stashes
git stashTracked 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 -pInteractive — choose hunks to stash
git stash --keep-indexStash 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 desk
  • git stash -m = Pack and label the box
  • git stash push file.js = Pack only specific items
  • git stash -p = Sort through and pack items one by one

Visual Architecture

flowchart TD CHANGES["💻 Uncommitted Changes"] --> BASIC["git stash<br/>(tracked only)"] CHANGES --> MSG["git stash -m 'msg'<br/>(with label)"] CHANGES --> UNTRACKED["git stash -u<br/>(+ untracked)"] CHANGES --> PARTIAL["git stash push file<br/>(specific files)"] CHANGES --> PATCH["git stash -p<br/>(interactive)"] style CHANGES fill:#1a1a2e,stroke:#e94560,color:#e94560 style MSG fill:#0f3460,stroke:#ffd700,color:#ffd700

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-index lets you stash unstaged work while keeping staged changes for testing.
  • Clean switches: Including untracked files (-u) ensures a fully clean workspace.

Code

bash
# ─── 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 refactor

Key Takeaways

  • Always use -m to label your stashes — unlabeled stashes become mystery boxes.
  • Use -u to include untracked files for a completely clean workspace.
  • Use git stash push <files> to stash only specific files.
  • Use git stash -p for 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-index do? 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.

Topics Covered

Git StashingGit Fundamentals

Tags

#git#stash#save#beginner-friendly

Last Updated

2026-02-12