git stash Command Guide
Stash the changes in a dirty working directory away
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Save
git stashStash changesgit stash -m "msg"Stash with messagegit stash -uInclude untrackedApply
git stash popApply and removegit stash applyApply and keepgit stash apply stash@{2}Apply specificManage
git stash listList all stashesgit stash showShow stash diffgit stash dropDelete stashDownloadable Image Preview
Basic Usage
The git stash command temporarily saves uncommitted changes, allowing you to switch branches or perform other operations with a clean working directory.
# Stash current changes
git stash
# Stash with a descriptive message
git stash -m "Work in progress on login"
# Apply the most recent stash
git stash popStashing Options
# Basic stash (tracked files only)
git stash
# Include untracked files
git stash -u
git stash --include-untracked
# Include untracked and ignored files
git stash -a
git stash --all
# Stash with message
git stash -m "WIP: feature X"
git stash push -m "WIP: feature X"
# Stash specific files
git stash push -m "Just these files" file1.js file2.js
# Interactive stash (select hunks)
git stash -pApplying Stashes
# Apply most recent stash and remove it
git stash pop
# Apply most recent stash but keep it
git stash apply
# Apply a specific stash
git stash apply stash@{2}
# Apply and keep the staged state
git stash apply --index
# Create branch from stash
git stash branch new-feature-branchpop vs apply
- pop: Apply + remove from stash list (use when done)
- apply: Apply + keep in list (use when testing)
Managing Stashes
# List all stashes
git stash list
# stash@{0}: WIP on main: abc1234 Last commit msg
# stash@{1}: On feature: def5678 Another commit
# Show stash contents (diff)
git stash show
git stash show -p # Full diff
git stash show stash@{2} # Specific stash
# Delete a stash
git stash drop # Drop most recent
git stash drop stash@{2} # Drop specific
# Delete all stashes
git stash clearPractical Examples
# Quick context switch
git stash -m "WIP: feature work"
git checkout hotfix
# ... fix the bug ...
git checkout feature
git stash pop
# Pull with local changes
git stash
git pull
git stash pop
# Move changes to a new branch
git stash
git checkout -b new-feature
git stash pop
# Test changes on another branch
git stash
git checkout other-branch
git stash apply # Keep stash for main branch
# ... test ...
git stash drop # Remove when done
git checkout main
git stash pop
# Stash only unstaged changes
git stash --keep-indexFrequently Asked Questions
What is the difference between stash pop and stash apply?
"git stash pop" applies the stash and removes it from the list. "git stash apply" applies but keeps the stash. Use apply if you want to apply the same stash to multiple branches.
How do I stash untracked files?
Use "git stash -u" or "git stash --include-untracked". By default, stash only saves tracked files. Add -a to also include ignored files.
Can I stash just some files?
Yes, use "git stash push -m "message" file1 file2" to stash specific files only. Or use "git stash -p" to interactively select what to stash.
How do I recover a dropped stash?
If you accidentally dropped a stash, use "git fsck --unreachable | grep commit" to find lost commits, then "git stash apply <sha>" to recover.
Can I create a branch from a stash?
Yes! Use "git stash branch <branch-name>" to create a new branch and apply the stash there. This is useful if applying the stash causes conflicts.
Summary
git stash is perfect for temporarily shelving changes. Always use descriptive messages and clean up old stashes regularly.
Quick Reference
git stash- Save changesgit stash pop- Apply and removegit stash list- List all stashesgit stash drop- Delete stash
Official Documentation
For authoritative information, refer to the official documentation: