Skip to content

Stash

Stash (git stash) temporarily shelves uncommitted changes — modified tracked files and staged changes — so you can switch branches or pull with a clean working tree, then restore them later.

Definition

What it is

git stash takes all modified tracked files and staged changes, saves them as a commit on a hidden stack (refs/stash), and resets the working tree to match HEAD. git stash pop applies the top stash and deletes it from the stack. git stash apply applies without deleting. git stash list shows the stack, each entry with a message (WIP on main: <hash> <subject>).

By default, stash ignores untracked files. git stash -u includes them; git stash -a also includes ignored files. Neither is the default because losing untracked work by accident is a common (and painful) mistake.

Why it matters

Git refuses to switch branches if uncommitted changes would conflict with the target. Stash solves that: shelve the changes, switch, do the urgent work, switch back, restore. Without stash, you would have to commit half-done work to the branch (noise in history), or copy-paste the changes manually (dangerous).

Stash vs commit vs worktree

Aspect

Stash

Commit

Worktree

Persistence

Stack, local

Branch

Filesystem

Visibility

git stash list

git log

Filesystem path

Sharing

No

Yes

No

Use case

Quick context switch

Permanent history

Long parallel work

Risk

Forgetting to pop

Clutter if WIP

Disk space

The stash stack

graph TD
  W[Working tree with changes] -->|stash push| S[Stash stack]
  S -->|stash pop| W2[Working tree restored]
  W2 -->|stash push| S2[Stash stack grows]
  S2 -->|stash drop| X[Dropped]
  style S fill:#E8F5E9
  style X fill:#FFEBEE

How to use stash well

Stash without regret

  1. Name your stashes

    git stash push -m "wip: fix login" beats WIP on main: <hash>. You will not remember which is which.

  2. Include untracked with `-u`

    git stash -u saves new files too. Without it, a new file is left behind and lost on next git clean -fd.

  3. Prefer `pop` over `apply`

    pop deletes from the stack; apply does not. apply accumulates duplicated stashes you never clean.

  4. Drop old stashes

    git stash clear wipes all. Review with git stash list first.

  5. Prefer worktrees for long work

    If you switch contexts for hours, use git worktree add instead of a long-lived stash.

Common failures

Origin

The word comes from mid-19th-century American English: a stash is a hidden store, originally of money or contraband. The verb "to stash" means to hide something for later. Git adopted the word in 2007 for its feature that hides working-tree changes on a stack, precisely mirroring the physical metaphor: a hidden place where you put something until you need it.

git stash is used to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

— git-stash(1) manual, 2024
2007 · git stash aparece en Git 1.5.3
2018 · `git stash push` con `-m` mensaje (Git 2.13)
2019 · `git stash push -u` para untracked (reafirmado)
0 · número de clonaciones para stashes (local)
2 · operaciones típicaspush / pop

Evidence

FAQ

What does stash exclude?

Untracked files by default. Use -u to include them, -a for ignored files too.

Can I recover a dropped stash?

Yes, with git fsck --no-reflog | awk '/dangling commit/ {print $3}' then git stash apply <hash>, but only if gc has not run.

Stash vs commit?

Stash is temporary and local; commit is permanent and shareable. Use stash for context switches, commit for history.

Stash vs worktree?

Stash shelves changes; worktree gives a second working directory. Use worktrees for hours-long parallel work, stash for minutes-long switches.

Connections

Etymology

Origin: borrowed

  • stash (noun): American English, mid-19th century, "a hidden store of money or supplies"

Semantic network

StashGitversion-coBranchCommitMergeRebaseworktree

● broader ● narrower ● related

Broader

  • Git
  • version-control

Related

Sources