Command Reference

git stash Tutorial

Explains how to temporarily shelve local changes with git stash and later inspect, restore, or remove stash entries.

Who This Is For
  • Readers pausing work without making a real commit yet
Prerequisites
  • Understanding that stash is not an all-purpose backup
  • Knowing the difference between apply and pop
Common Risks
  • Assuming stash preserved everything automatically
  • Letting the stash stack become untracked long-term context

The short version

git stash temporarily shelves your uncommitted work so you can switch tasks and come back later.

The typical stash cycleThe best use of stash is usually short-lived interruption handling: save the current state, switch context, then deliberately re-apply the work when you are ready.
Working treeedited files
stash push
Stash stackstash@{0} / stash@{1}
apply / pop
Resume workresume work

The most useful framing is to treat stash as a short-term holding area, not as a long-term archive or branch replacement.

Good use cases

  • you need to jump to another branch quickly
  • the work is not ready to commit yet
  • you want to preserve a short-lived experiment without keeping it in the working tree

Common operations

Save current work

git stash push -m "wip: login form"

Inspect stash entries

git stash list

Reapply the latest stash

git stash apply

Reapply and remove it

git stash pop

Inspect what a stash contains

git stash show -p stash@{0}

Two advanced but very practical patterns

Include untracked files

git stash push -u -m "wip: include new files"

Stash only part of the tree

git stash push src/checkout.ts

apply vs pop

  • apply: restore but keep the stash entry
  • pop: restore and delete it if successful

If you are not fully sure yet, apply is usually the safer option.

That is why the diagram highlights apply / pop as a separate step. Many stash mistakes happen there: pop is convenient, but it bundles “restore the changes” with “try to remove the stash entry,” while apply keeps the recovery path more conservative.

A safer restore workflow

Instead of immediately using pop, a safer sequence is often:

  1. git stash list
  2. git stash show -p stash@{0}
  3. git stash apply stash@{0}
  4. confirm the result
  5. git stash drop stash@{0}

Two practical habits

Always add a message

git stash push -m "wip: checkout flow"

That makes git stash list much easier to read later.

Prefer apply before deleting

For important work, restoring first and deleting later is often safer than using pop immediately.

Why stash restores can still conflict

Stash records a set of changes, but the surrounding code may have changed by the time you reapply it. That is why apply and pop can still produce conflicts.

A practical decision rule

  • use stash for short interruptions
  • use branches for work that lives longer than a quick context switch
  • use commits and branches for anything reviewable or shared

What problem this command solves in a workflow

git stash solves the problem of "I need to temporarily set aside my uncommitted work so I can switch branches and come back later." It saves both the working tree and index state onto a stack, letting you park unfinished changes without committing them.

Typical use cases

  • When you need to jump to another branch quickly but your current work is not ready to commit, use git stash push to save the state and get a clean working tree.
  • Inspect saved stashes with git stash list and review their content with git stash show -p stash@{0} before reapplying.
  • Use git stash push -u to also include untracked files in the stash entry, so nothing is left behind when you switch context.

Diagram view

Temporarily shelving uncommitted changesstash captures the working tree and index state as a commit-like object on a stack, letting you restore it later with apply or pop.
Input
Working tree changesIndex stateUntracked files (with -u)
Output
Stash entry on stackClean working treeSaved index state
Treat stash as a short-term holding area, not a long-term archive — use branches for work that lives longer than a quick context switch.

Special cases and boundaries

  • Stash records a set of changes, but the surrounding code may have changed by the time you reapply it — apply and pop can still produce conflicts.
  • Use apply to restore changes while keeping the stash entry as a safety net, or pop to restore and remove the entry in one step.
  • Untracked files only travel with the stash entry when you use -u (or -a for ignored files too).
  • For important work that will continue for hours or days, a branch is usually a better fit than a growing stash pile.

Try it yourself

Exercise: apply first, then decide whether to drop

The fragile moment in stash is usually restore time, not save time. This drill turns the safer habit into a repeatable routine.

Setup
git switch -c lab/stash-demo
# edit one tracked file and create one untracked file
Try it
  1. Run `git stash push -u -m "wip: stash demo"`.
  2. Inspect the entry with `git stash show -p stash@{0}`.
  3. Restore with `git stash apply stash@{0}` first, and only then decide whether to run `git stash drop stash@{0}`.
What happens next
  • You will see that untracked files only travel with stash when you use `-u`.
  • `apply` restores the work while keeping the stash entry as a safety net.
  • Dropping after inspection is more conservative than going straight to `pop`.
Common mistake checks
  • If the work will continue for hours or days, a branch is usually a better fit than a growing stash pile.
  • Do not jump to `pop` before checking what the stash actually contains.
  • A conflict during apply usually means the surrounding code changed, not that the stash is broken.