Software development is full of interruptions: you are deep in the middle of writing an unfinished feature when an urgent production bug alert arrives. Your current code is broken and half-written—you can't commit it, but you also can't lose it. The git stash command is your temporary clipboard.
1. How Git Stash Works
git stash takes the dirty state of your working directory (all modified tracked files and staged changes) and saves it onto an internal stack of unfinished changes, returning your working directory to a clean HEAD state!
1[ Working Directory: Broken, half-finished code ]
2 |
3 git stash
4 |
5 v
6[ Stash Stack ] <------------------ [ Clean Working Directory ]
7 stash@{0}: WIP on feature-cart (Ready to switch branches & fix bug!)
2. Core Stash Commands
A. Stashing Your Changes
1# Quick stash
2git stash
3
4# Stash with a descriptive message (Recommended!)
By default, git stash only stashes tracked files. If you created brand new files that haven't been committed before, add the -u (or --include-untracked) flag:
1git stash -u
3. Inspecting and Restoring Stashes
A. View All Saved Stashes (git stash list)
1git stash list
Output:
1stash@{0}: On feature-cart: WIP: half-implemented payment modal
2stash@{1}: On main: WIP: temporary database connection string
B. Restoring Changes: git stash pop vs. git stash apply
Command
Restores Changes to Working Tree?
Removes Stash from Stack?
git stash pop
Yes
Yes (deletes the stash item if no conflicts occur)
git stash apply
Yes
No (leaves the stash on the stack so you can reuse it elsewhere)
1# Apply most recent stash and remove it from stack
2git stash pop
3
4# Apply a specific older stash by index without removing it
5git stash apply stash@{1}
4. Cleaning Up Stashes
Stashes accumulate over time and consume disk space. Clean them up regularly:
1# 1. Drop a specific stash from the stack
2git stash drop stash@{0}
3
4# 2. Clear ALL stashes completely (irreversible!)
5git stash clear
5. Typical Real-World Scenario
1
Working on feature-redesign, edited 3 files. Code doesn't compile yet.