Git Stash: Save Work for Later
The Problem
You're working on a feature, but suddenly need to switch branches to fix a bug. Your changes aren't ready to commit. What do you do?
Git stash saves your uncommitted changes and gives you a clean working directory.
How It Works
Working Directory (dirty) Stash Stack
┌──────────────────┐ ┌──────────────────┐
│ Modified files │──stash→│ stash@{0}: latest │
│ Staged changes │ │ stash@{1}: older │
└──────────────────┘ └──────────────────┘
↑ │
└────── stash pop ──────────┘
Essential Commands
# Save current changes to the stash
git stashSave with a description
git stash save "work on navbar"List all stashes
git stash listRestore the most recent stash
git stash popRestore without removing from stash
git stash applyRemove a specific stash
git stash drop stash@{0}Clear all stashes
git stash clear
Real-World Scenario
# You're working on a feature...
echo "new feature" >> feature.js
git add feature.jsBoss says: "Fix the homepage bug NOW!"
git stash save "feature work in progress"Now your directory is clean - switch branches
git checkout main
... fix the bug, commit, push ...
Come back to your feature
git checkout feature-branch
git stash pop
Your changes are back!
Stash with Untracked Files
By default, git stash only saves tracked files. To include new files:
git stash -u # Include untracked files
git stash -a # Include ALL files (even ignored ones)
Summary
| Command | Purpose |
|---|---|
| <code>git stash</code> | Save uncommitted changes |
| <code>git stash pop</code> | Restore and remove from stash |
| <code>git stash apply</code> | Restore but keep in stash |
| <code>git stash list</code> | See all stashed changes |
| <code>git stash drop</code> | Delete a stash entry |