iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

git stash

git stash shelves uncommitted changes so the working tree is clean. Switch branches, pull, rebase — then pop the stash back. The bridge between “in-progress” and “urgent context switch.”

Save, list, pop, partial, untracked

EXAMPLE
# 1) Basic — stash everything tracked
git stash                                # message defaults to branch + commit
git stash push -m 'wip: refactor auth'   # named

# Now the working tree matches HEAD; switch branches freely.

# 2) Apply later
git stash list                           # see all stashes
# stash@{0}: WIP on feature/auth: abc1234 fix typo
# stash@{1}: On main: refactor session helper

git stash pop                            # apply most recent, drop from list
git stash apply                          # apply most recent, KEEP in list
git stash pop  stash@{1}                 # apply a specific one
git stash drop stash@{0}                 # delete without applying

# 3) Include untracked files
git stash -u                              # untracked too
git stash -a                              # untracked + ignored

# 4) Stash only some files (path-based)
git stash push -m 'wip' -- src/auth.ts src/auth.test.ts

# 5) Stash a partial change — interactive
git stash push -p                         # 'p' for patch; choose hunks

# 6) See what's in a stash before applying
git stash show                            # summary
git stash show -p                          # full diff
git stash show -p stash@{1}

# 7) Stash with the keep-index trick — used in pre-commit testing
git stash push --keep-index               # leave staged changes IN the work tree
# Run tests on just the staged change; if they pass, commit, then pop the rest.

# 8) Convert a stash to a branch — most reliable recovery
git stash branch hotfix-restore stash@{0}
# Creates a new branch from the original HEAD, applies the stash, drops it.

# 9) Clear ALL stashes (destructive)
git stash clear

# 10) When NOT to use stash
#   • Don't use stash for things that should be a commit (3+ hour work) — commit a WIP
#     instead. WIP commits are easier to recover and survive crashes.
#   • Don't pile up stashes — they're easy to forget; the work bit-rots.
#   • Don't stash across machines — stash is local-only.

# 11) Better workflows for in-progress work
# a) WIP commits + rebase later
git add -A; git commit -m 'wip'        # cheap, recoverable
git rebase -i origin/main             # squash before pushing

# b) Worktrees — separate working directories per branch (no stash needed)
git worktree add ../my-app-hotfix hotfix-branch
# Edit in ../my-app-hotfix without touching your current tree

# 12) Recover a 'lost' stash
git fsck --unreachable | grep commit
# Each stash is a commit; even after `stash drop`, it's in the reflog for ~30 days.
git stash apply <SHA>

# 13) Configure
git config --global stash.showStat true     # `git stash show` shows file stats by default
git config --global stash.showPatch true    # full patch

# 14) Real day-to-day pattern
#   urgent ticket lands → git stash push -m 'wip: PR-242' → switch → fix → commit → push → git switch back → git stash pop
# 30 seconds; never lose context.

Why it matters

Use git stash push -m \"...\" with a real message — un-named stashes pile up unintelligibly. For anything > 2 hours, commit WIP instead; commits survive crashes, branches, and laptops better than stashes do.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
git stash         # save WIP
git stash pop      # restore + drop
git stash list
Try it Yourself »

Exercise

Save uncommitted work to come back to later.

git

Discussion

Loading…