CurriculumAdvanced Gitgit stash — Temporary Storage
Lesson 3 of 650%
Advanced7 min+70 XP

git stash — Temporary Storage

Stash saves your uncommitted changes to a temporary stack, cleans your working directory to match the last commit, and lets you restore everything later. Perfect for context-switching without polluting history with a "WIP" commit.

01.The Stash Stack

The stash works like a stack (LIFO — last in, first out). Each stash push creates a new entry. git stash pop applies and removes the most recent entry. git stash apply applies it but keeps it in the stack (useful for applying the same stash to multiple branches). You can have many stashes — always name them so you know what each one contains.

Git's Three Areas
📁
Working Dir
Your files
→ git add
📋
Staging Area
Prepared
→ git commit
🗄️
Repository
.git/ history

02.The Classic Context-Switch Scenario

You are halfway through a feature when Slack pings: critical bug in production, fix it now. Your current work is not ready to commit. Solution: git stash (saves WIP, cleans directory), git checkout main, create fix branch, fix the bug, PR and merge, git checkout feature/mine, git stash pop (restores your work exactly). Zero commits wasted.

Commit Timeline
a1b2c3d
init
b2c3d4e
feat
c3d4e5f
fix
d4e5f6a
HEAD

Commands

commands — click to reveal output
$
git stash
Stash all staged and unstaged changes
▼ show output
$
git stash push -m "half-done login validation"
Stash with a descriptive label (highly recommended)
$
git stash list
View all stash entries with their indices
▼ show output
$
git stash pop
Apply most recent stash and remove it from the stack
$
git stash apply stash@{1}
Apply a specific stash entry (keeps it in the stack)
$
git stash drop stash@{1}
Delete a specific stash entry without applying it
$
git stash clear
Permanently delete ALL stash entries — use carefully
$
git stash branch feature/wip stash@{0}
Create a new branch from a stash and apply it
KNOWLEDGE CHECK

What is the difference between git stash pop and git stash apply?

Rate this lesson:
PreviousNext Lesson