Git Stash: Save, Apply, and Manage Work in Progress
Complete guide to git stash: save changes temporarily, apply or pop stashes, manage multiple stashes, stash specific files, and handle stash conflicts.
Detailed Explanation
Complete Guide to Git Stash
git stash temporarily shelves changes so you can switch context (branches, tasks) without committing incomplete work.
Basic Stash Operations
# Stash all tracked changes
git stash
# Stash with a descriptive message
git stash push -m "WIP: user authentication form"
# Stash including untracked files
git stash -u
# Stash everything (including ignored files)
git stash -a
Retrieving Stashed Changes
# Apply the most recent stash and remove it
git stash pop
# Apply without removing from stash list
git stash apply
# Apply a specific stash
git stash apply stash@{2}
Managing Multiple Stashes
# List all stashes
git stash list
# Show what is in a stash
git stash show stash@{0}
# Show the diff of a stash
git stash show -p stash@{0}
# Drop a specific stash
git stash drop stash@{1}
# Clear all stashes
git stash clear
Stash Specific Files
# Stash only specific files (git 2.13+)
git stash push -m "config changes" src/config.ts src/env.ts
# Use interactive mode to select hunks
git stash push -p
Create a Branch from a Stash
# Create a new branch and apply the stash
git stash branch feature/from-stash
This is useful when the stash conflicts with changes on the current branch.
Handling Stash Conflicts
When git stash pop or apply causes conflicts:
# Resolve conflicts manually, then:
git add <resolved-files>
# The stash is NOT automatically dropped on conflict
# Drop it manually after resolving:
git stash drop
Best Practices
- Always add a message with
-m-- stashes without descriptions are hard to identify later - Do not accumulate stashes -- they are meant to be temporary
- Prefer branches for longer-lived work-in-progress
- Use
-uto include untracked files when stashing
Use Case
Git stash is indispensable when you need to switch context quickly. Common scenarios include: a teammate asks for a code review and you have uncommitted changes; a critical bug report comes in while you are mid-feature; you want to test something on a clean working tree; or you need to pull changes but have local modifications. Stashing lets you shelve work instantly without creating throwaway commits.