Stage 2 · Tools
Git Fundamentals
Stashing
Park work-in-progress safely, apply, pop, and manage a stash stack.
What git stash Does
git stash takes your staged and unstaged changes, saves them on a temporary stack, and reverts your working tree and index to match HEAD. Your working tree becomes clean — as if you had not made any changes. You can then switch branches, pull updates, or do other work, and come back to your changes later.
Think of stashes as a stack of dirty working states. Each git stash push pushes a new state onto the stack. git stash pop pulls the top state off and applies it. git stash apply copies the top state without removing it from the stack.
# Working tree has uncommitted changes
git status
# modified: app.js
# modified: config.yaml
# Save changes to the stash
git stash
# Saved working directory and index state On main: WIP on main: a4b8c3d add login
# Working tree is now clean
git status
# nothing to commit, working tree clean
# Switch branches, do other work, then come back
git checkout feature-branch
# ... work on feature branch ...
git checkout main
# Restore your stashed changes
git stash popThe stash message includes the branch name and the commit message of HEAD at the time of stashing. This gives you context when reviewing a long list of stashes later.
git stash push
git stash push (or just git stash) creates a new stash entry. Without any flags, it saves both staged and unstaged changes. Use flags to control what gets stashed.
# Stash with a descriptive message
git stash push -m "WIP: halfway through refactor"
# Stash only tracked file changes (exclude new files)
git stash --keep-index
# Stash only specific files
git stash push -m "auth changes" -- app.js lib/auth.ts
# Include untracked files
git stash -u
# Include ignored files (build artifacts, etc.)
git stash -aThe -m flag adds a message so you can identify the stash later. --keep-index stashes unstaged changes but leaves staged changes staged — useful when you have already staged the commits you want and need to shelve the rest.
Each stash is a commit stored in .git/refs/stash. A stash has up to three parent commits: the HEAD commit at the time, the index state, and (if --include-untracked was used) the untracked files. This is why git stash pop can restore all three states.
git stash list
git stash list shows all stashes in the stack, with the most recent stash at the top. Each entry shows the stash name, the reflog pointer, and the stash message.
git stash list
# stash@{0}: On main: WIP: halfway through refactor
# stash@{1}: On main: add error handling
# stash@{2}: On feature: temp save before rebase
# Reference a specific stash
git stash apply stash@{2}
# Drop a specific stash
git stash drop stash@{1}The stash@{n} syntax uses reflog notation. stash@{0} is the most recent stash, stash@{1} is the one before, and so on. You can reference stashes by index to manage specific entries in the stack.
pop vs apply
git stash pop applies the most recent stash and removes it from the stack. git stash apply applies the stash but keeps it on the stack. If a conflict occurs, pop will not remove the stash — it stays on the stack so you can resolve the conflict and try again.
# Pop: apply + remove (most common)
git stash pop
# Apply: copy only (stash stays on stack)
git stash apply
# Apply a specific stash without removing it
git stash apply stash@{1}
# If pop has conflicts, the stash is preserved
git stash pop
# CONFLICT (content): Merge conflict in app.js
# The stash entry is NOT removed — resolve and retryUse pop when you are done with the stash and want to clean up. Use apply when you need to apply the same stash to multiple branches, or when you want a safety net in case of conflicts.
git stash show
git stash show displays which files changed in a stash and a summary of the changes. Use -p to see the full diff.
# Summary of changes in most recent stash
git stash show
# Full diff of most recent stash
git stash show -p
# Show changes in a specific stash
git stash show -p stash@{2}
# List files changed
git stash show --name-onlyReview a stash before applying it to make sure it is the right one. The -p flag shows the exact changes so you know what will be applied to your working tree.
Stash Best Practices
| Good Use | Bad Use |
|---|---|
| Quick context switch: stash, switch branch, pop | Long-term storage: use a WIP branch instead |
Saving incomplete work before git pull | Hiding work you want to forget about |
Stashing specific files with -m message | Stashing without a message and losing track |
| Applying stash to a clean working tree | Applying stash on top of uncommitted changes |
If you find yourself stashing and re-stashing without popping, you probably need a WIP branch. Create it, commit your incomplete work, and rebase it later. Branches have history, pull requests, and CI. Stashes are for temporary pauses.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.