Ad – 728×90
⚙️ Advanced Git

git stash – Temporarily Saving Uncommitted Work

You're halfway through building a new feature when an urgent bug report lands in your inbox. You can't commit half-done work, and you can't just throw it away. git stash is your escape hatch — it saves all your uncommitted changes to a temporary stack so you can switch context instantly, fix the bug, and come right back to where you left off.

⏱️ 15 min read 🎯 Intermediate 📅 Updated 2026

What Is git stash?

A stash is a snapshot of your working directory and staging area saved onto a stack. The stack persists across branch switches and is stored in .git/refs/stash. Stashing lets you:

  • Save work-in-progress without creating a messy commit
  • Switch to another branch with a clean working tree
  • Carry half-done changes across branches
  • Store multiple independent sets of changes (named stashes)
ℹ️
The scenario

You're building feature/login-form and have modified 4 files. Your team lead calls — there's a critical bug on main. You need a clean working tree to switch branches. Don't panic, just stash.

Basic Stash Commands

The simplest form saves all tracked changes (both staged and unstaged) and reverts your working tree to match HEAD:

Bash
# Stash all tracked changes (staged + unstaged)
git stash

# Stash with a descriptive message (highly recommended)
git stash push -m "WIP: login form validation"

# Stash including untracked files (new files not yet staged)
git stash push --include-untracked -m "WIP: login form + new helpers"

# Stash everything including ignored files
git stash push --all -m "full working tree backup"
▶ Output
Saved working directory and index state On feature/login-form: WIP: login form validation

After stashing, git status shows a clean working tree. You can now safely switch branches.

Bash
# Now you can safely switch branches
git switch main
git switch -c hotfix/null-pointer
# ... fix the bug, commit, merge ...
# Come back to your feature branch
git switch feature/login-form

Listing Your Stashes

Stashes are numbered from newest (0) to oldest. Use git stash list to see all saved stashes:

Bash
git stash list
▶ Output
stash@{0}: On feature/login-form: WIP: login form validation
stash@{1}: On feature/dashboard: WIP: chart component
stash@{2}: WIP on main: 3f2a1b9 Fix typo in README

You can inspect the contents of a specific stash:

Bash
# Show a summary of what's in stash@{0}
git stash show

# Show the full diff of stash@{1}
git stash show -p stash@{1}
Ad – 336×280

Applying a Stash Back

There are two ways to re-apply a stash:

CommandWhat it doesStash entry afterwards
git stash popApply most recent stashRemoved from stash list
git stash applyApply most recent stashKept in stash list
git stash pop stash@{2}Apply specific stashRemoved from stash list
git stash apply stash@{1}Apply specific stashKept in stash list
Bash
# Apply the most recent stash and remove it from the stash list
git stash pop

# Apply a specific stash but KEEP it in the list (useful for applying to multiple branches)
git stash apply stash@{1}

# Create a new branch from a stash — branch out AND pop at once
git stash branch feature/continuation stash@{0}
💡
When to use apply vs pop

Use git stash apply when you want to apply the same stash to multiple branches (e.g., applying a common fix to both main and a release branch). Use git stash pop for the normal flow: apply once and clean up.

Partial Stash — Choose What to Save

Sometimes you only want to stash part of your changes. The -p (patch) flag lets you interactively choose which hunks to stash:

Bash
# Interactive mode — choose which hunks to stash
git stash push -p -m "only stash the auth changes"

# Git will show each changed hunk and ask:
# y = stash this hunk
# n = keep this hunk in working tree
# s = split hunk into smaller pieces
# q = quit (stash what's been selected so far)
# ? = help

You can also stash specific files only:

Bash
# Stash only specific files
git stash push -m "WIP: auth only" -- src/auth.js src/login.js

Cleaning Up Stashes

The stash stack can grow messy over time. Prune it regularly:

Bash
# Delete a specific stash entry
git stash drop stash@{0}

# Delete ALL stashes at once (no undo!)
git stash clear

# Verify the stash list is empty
git stash list
# (no output = stash list is empty)
⚠️
git stash clear is permanent

git stash clear deletes all stash entries and cannot be undone with a simple command. If you accidentally clear stashes, they may still be recoverable via git fsck --unreachable followed by git stash apply <dangling-blob-hash> — but only if garbage collection hasn't run yet.

📋 Summary

  • git stash saves all tracked changes and reverts your working tree to HEAD — perfect for urgent context switches.
  • Use git stash push -m "message" to always label your stashes for clarity.
  • git stash list shows stash@{0} (newest) through stash@{N} (oldest).
  • git stash pop applies the most recent stash and removes it; git stash apply stash@{N} applies and keeps it.
  • git stash push -p lets you interactively pick which hunks to stash (partial stash).
  • --include-untracked is needed to stash new files not yet staged.
  • git stash branch <name> creates a new branch from the stash — clean way to continue work in a dedicated branch.
  • git stash drop / git stash clear — clean up old stashes regularly.

FAQ

Does git stash work across branches? +

Yes. Stashes are stored in a repository-wide stack and are not tied to any specific branch. You can stash on feature/login, switch to main, and pop the stash there. However, if the stashed changes conflict with the target branch's files, you'll need to resolve merge conflicts just like a normal merge.

Why are my new (untracked) files not being stashed? +

By default, git stash only saves changes to tracked files. Newly created files that haven't been staged with git add are untracked and are ignored by the default stash. Use git stash push --include-untracked (or -u shorthand) to include them. Use --all to also include files listed in .gitignore.

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

git stash pop applies the stash and immediately removes it from the stash list — it's the most common operation. git stash apply applies the stash but leaves it in the list, so you can apply it again to another branch if needed. If pop encounters a conflict, it still removes the stash entry, so you may want to use apply when conflicts are likely, giving you a chance to keep the stash as a safety net.

Can I have a stash conflict? +

Yes. If the stashed changes conflict with the current branch state, Git will mark the files with conflict markers just like a merge conflict. Resolve the conflicts manually, then git add the resolved files. You do not need to run any "continue" command — the stash application is complete once you resolve the files.

Is git stash a good substitute for commits? +

No. Stashes are temporary and local — they are not pushed to remote repositories and they can be accidentally lost with git stash clear. For saving work at end-of-day or sharing work-in-progress, prefer a WIP: commit on your feature branch that you can amend or squash later. Stashes are best for very short context switches (minutes to hours).