Stashing Work in Progress

Reviewed & published by Brayan K

An urgent bug always strikes mid-feature. git stash lets you shelve your half-finished work, deal with the interruption on a clean tree, then restore everything exactly as it was. By the end you'll stash, list, pop, apply, and drop with confidence.

Part of the free Git course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

What You'll Learn

1️⃣ git stash — Shelve and Restore

git stash takes your uncommitted (tracked) changes off the working tree and stores them safely, leaving a clean tree behind. You can then switch branches freely. When you return, git stash pop puts everything back exactly as it was and removes the stash. It's the perfect interruption handler.

# Scenario: you're mid-change on a feature, but an urgent bug needs fixing
# on another branch. You don't want a half-finished commit — so shelve it.

# 1) Shelve your uncommitted (tracked) changes and clean the working tree
git stash

# 2) Your working tree is now clean — switch branches and fix the bug
git switch main
git commit -am "Fix urgent bug"

# 3) Come back and restore your shelved work exactly as it was
git switch feature
git stash pop

2️⃣ Managing the Stash Stack

Stashes form a last-in, first-out stack: the newest is always stash@{0}. git stash list shows them all. apply restores a stash but keeps it on the stack; pop restores and removes it. drop deletes one without restoring, and clear wipes them all.

# Stashes form a STACK (last-in, first-out). The newest is stash@{0}.

# Show every stash you've saved
git stash list

# Restore the latest stash but KEEP it on the stack (reusable)
git stash apply

# Restore the latest stash AND remove it from the stack
git stash pop

# Delete the latest stash WITHOUT restoring it
git stash drop

# Delete ALL stashes
git stash clear

3️⃣ Stashing Untracked Files (-u)

Here's the gotcha that catches everyone: plain git stash does not stash untracked files — brand-new files Git isn't following yet. They stay behind in your working tree. Add -u (or --include-untracked) to shelve those too, so the tree is truly clean.

# ⚠️ Gotcha: plain git stash does NOT include UNTRACKED files
# (brand-new files Git isn't following yet). They stay in your working tree.

# This file is new and untracked
echo "draft" > new-feature.txt

# Plain stash leaves new-feature.txt behind
git stash
git status -s            # still shows: ?? new-feature.txt

# Use -u (or --include-untracked) to stash untracked files too
git stash -u
git status -s            # now clean — new-feature.txt is stashed as well

Your turn. Shelve a change, check the shelf, and bring it back. Fill in the three blanks.

# 🎯 YOUR TURN — replace each ___ then run it in your terminal.
# Goal: shelve work, switch away, and restore it.

echo "half done" >> feature.js

# 1) Shelve your changes so the working tree is clean
git ___                        # 👉 the command that shelves changes

# 2) Check what's on the shelf
git stash ___                  # 👉 the subcommand that shows all stashes

# 3) Bring your work back AND remove it from the shelf
git stash ___                  # 👉 the subcommand that restores + drops

# ✅ Expected: step 1 cleans the tree, step 2 shows "stash@{0}: WIP ...",
#    step 3 restores feature.js and reports "Dropped refs/stash@{0}".

Common Errors (and the fix)

Pro Tips

📋 Quick Reference

CommandPurpose
git stashShelve tracked changes, clean the tree
git stash -uStash untracked files too
git stash listShow all stashes (stack)
git stash popRestore latest stash and remove it
git stash applyRestore latest stash and keep it
git stash dropDelete a stash without restoring
git stash clearDelete all stashes

Mini-Challenge: Juggle Two Stashes

No commands given this time — just the plan. Stack two stashes, target them by name, and clean up.

# 🎯 MINI-CHALLENGE: juggle two stashes.
# No commands given — just the plan. Run each step in your terminal.
#
# 1. Edit a file, then "git stash" it (call it stash A)
# 2. Edit a DIFFERENT file, then "git stash" it (stash B, now stash@{0})
# 3. Run "git stash list" — confirm B is stash@{0} and A is stash@{1}
# 4. Use "git stash apply stash@{1}" to bring A back WITHOUT removing it
# 5. Clean up with "git stash drop stash@{1}" and "git stash pop"
#
# ✅ Expected: after step 5, both edits are restored and "git stash list"
#    is empty.

# your commands here

🎉 Lesson Complete!

Practice quiz

What does `git stash` do?

  • Deletes your uncommitted changes
  • Commits your changes
  • Shelves your uncommitted changes and cleans the working directory
  • Pushes to the remote

Answer: Shelves your uncommitted changes and cleans the working directory. git stash saves your uncommitted work onto a stack and resets the working tree to a clean state.

By default, does plain `git stash` include UNTRACKED files?

  • No — only tracked changes are stashed by default
  • Yes, always
  • Only files over 1MB
  • Only files in subfolders

Answer: No — only tracked changes are stashed by default. Plain git stash leaves untracked files alone; use -u to include them.

Which flag stashes untracked files along with tracked changes?

  • git stash -a
  • git stash -t
  • git stash --all-tracked
  • git stash -u

Answer: git stash -u. git stash -u (or --include-untracked) also shelves untracked files.

What does `git stash pop` do?

  • Lists all stashes
  • Restores the latest stash AND removes it from the stack
  • Deletes the latest stash without restoring
  • Renames a stash

Answer: Restores the latest stash AND removes it from the stack. pop applies the most recent stash and then drops it from the list.

How does `git stash apply` differ from `git stash pop`?

  • apply restores but KEEPS the stash in the list
  • apply deletes the stash
  • apply only works once
  • There is no difference

Answer: apply restores but KEEPS the stash in the list. apply restores the changes but leaves the stash on the stack, so you can reuse it.

Which command shows all your saved stashes?

  • git stash show-all
  • git stash log
  • git stash list
  • git stash -l

Answer: git stash list. git stash list prints the stack, each entry labelled like stash@{0}.

How are stashes ordered on the stack?

  • Oldest first
  • Last-in, first-out — the newest is stash@{0}
  • Alphabetically
  • Randomly

Answer: Last-in, first-out — the newest is stash@{0}. Stashes form a LIFO stack; stash@{0} is the most recent one you saved.

What does `git stash drop` do?

  • Restores a stash
  • Lists stashes
  • Creates a branch from a stash
  • Deletes a stash WITHOUT restoring it

Answer: Deletes a stash WITHOUT restoring it. drop removes a stash entry from the stack without applying it.

A common reason to stash is...

  • To compress the repo
  • To switch branches quickly without committing half-finished work
  • To delete a branch
  • To rename commits

Answer: To switch branches quickly without committing half-finished work. Stashing gives you a clean working tree so you can switch branches and come back later.

After `git stash` succeeds, what does `git status` typically show?

  • A merge conflict
  • All your changes still there
  • A clean working tree (nothing to commit)
  • An error

Answer: A clean working tree (nothing to commit). Stashing removes the changes from the working tree, leaving it clean.

Continue this course

Frequently asked questions

What is git stash for?

git stash shelves your uncommitted changes onto a stack and resets your working tree to a clean state, so you can switch branches, pull updates, or test something without making a half-finished commit. When you're ready, you restore the shelved changes with git stash pop (or apply) and pick up exactly where you left off.

Why didn't my new file get stashed?

Plain git stash only shelves tracked changes — files Git is already following. Brand-new, untracked files are left in your working tree. To include them, use git stash -u (short for --include-untracked). There's also git stash -a (--all) to additionally stash ignored files, though that's rarely needed.

What's the difference between git stash pop and git stash apply?

Both restore the changes from a stash into your working tree. The difference is cleanup: pop removes the stash from the stack after restoring it, while apply leaves it on the stack so you can reuse it elsewhere. Use apply when you want the same stashed changes on more than one branch, then git stash drop when you're finally done with it.

Can I have more than one stash at a time?

Yes. Stashes form a last-in, first-out stack. Each git stash adds a new entry, with the most recent labelled stash@{0}, the next stash@{1}, and so on. git stash list shows them all, and you can apply or pop a specific one by name, e.g. git stash apply stash@{1}.

Is stashing safe — can I lose work?

Stashing itself is safe; your changes are stored in the stash, not deleted. The risk is forgetting them: a stash can sit on the stack for weeks. Run git stash list periodically. Also avoid git stash clear or git stash drop unless you're certain, because those remove stashes without restoring them, and that work is then hard to recover.

Related lessons