How can I delete all of my Git stashes at once with a single command?
You can delete all Git stashes at once using the git stash clear command, which removes all stashed changes from your repository without confirmation.
Contents
- Understanding the
git stash clearCommand - Alternative Methods for Deleting Stashes
- Safety Precautions Before Clearing Stashes
- Practical Examples and Scenarios
- Recovering Accidentally Cleared Stashes
Understanding the git stash clear Command
The git stash clear command is the most direct way to remove all stashed changes from your repository. This is a destructive operation that cannot be undone, so it’s important to understand what it does before using it.
What Happens When You Run git stash clear
When you execute git stash clear, Git:
- Removes all stashed changes from the current branch
- Updates the stash reference to show no stashes
- Does not create a new commit
- Cannot be reversed (unless you have a backup)
Basic Usage
git stash clear
This command will delete all stashes immediately without prompting for confirmation.
Alternative Methods for Deleting Stashes
While git stash clear is the most direct method, there are several alternative approaches depending on your specific needs.
Deleting Individual Stashes
If you want to be more selective, you can delete stashes one by one:
# Delete the most recent stash
git stash drop
# Delete a specific stash by index (starting from 0)
git stash drop stash@{0}
git stash drop stash@{1}
# Delete multiple specific stashes
git stash drop stash@{0} stash@{2}
Using git stash drop with Selection
First, list your stashes to see what you have:
git stash list
Output might look like:
stash@{0}: WIP on feature-branch: 123abc4 Add user authentication
stash@{1}: WIP on main: 567def5 Update documentation
stash@{2}: WIP on bugfix: 890ghi6 Fix login validation
Then selectively delete:
# Keep only the most recent stash
git stash drop stash@{0}
git stash drop stash@{1}
Using Shell Loops for Batch Deletion
For more control, you can use shell scripting to delete stashes based on criteria:
# Delete all stashes except the most recent one
git stash list | head -n -1 | cut -d: -f1 | xargs git stash drop
# Delete stashes matching a pattern
git stash list | grep "old-feature" | cut -d: -f1 | xargs git stash drop
Safety Precautions Before Clearing Stashes
Since git stash clear is irreversible, it’s crucial to take proper precautions before using it.
Step 1: Review Your Stashes
Always check what stashes you have before clearing them:
git stash list
This will show you all stashed changes and help you determine if any are important to keep.
Step 2: Consider Backup Options
Before clearing all stashes, consider backing them up:
# Export stashes to individual branches for backup
git stash list | while read stash; do
git stash branch "backup-$(echo $stash | cut -d: -f2 | tr ' ' '-')" "$stash"
done
Step 3: Test on a Non-Critical Repository
If you’re unsure about the command, test it on a repository where it doesn’t matter if you lose stashes.
Step 4: Use Interactive Confirmation
While git stash clear doesn’t have a built-in confirmation, you can create an alias that does:
# Add to your .gitconfig or .bashrc
alias git-stash-clear='echo "WARNING: This will delete ALL stashes permanently. Continue? (y/N)" && read confirm && [ "$confirm" = "y" ] && git stash clear'
Practical Examples and Scenarios
Scenario 1: Cleaning Up After Feature Development
After completing a feature branch, you might have multiple stashes:
# Check existing stashes
git stash list
# stash@{0}: WIP on feature-x: 123abc4 Implementation
# stash@{1}: WIP on feature-x: 567def5 Refactoring
# stash@{2}: WIP on feature-x: 890ghi6 Testing
# Clean up all feature-related stashes
git stash clear
Scenario 2: Removing Temporary WIP Stashes
When working on multiple tasks, you might accumulate many Work-In-Progress stashes:
# List all stashes with dates
git stash list --date=short
# stash@{0}: WIP on main: 2024-01-15 Add caching
# stash@{1}: WIP on main: 2024-01-14 Fix UI bugs
# stash@{2}: WIP on main: 2024-01-13 Update dependencies
# Remove all temporary WIP stashes older than a week
git stash list | grep "WIP on main" | head -n -3 | cut -d: -f1 | xargs git stash drop
Scenario 3: Git Housekeeping Routine
Include stash clearing in your regular repository maintenance:
#!/bin/bash
# cleanup-repo.sh
echo "Repository cleanup started..."
echo "Current stashes:"
git stash list
echo "Removing all stashes..."
git stash clear
echo "Cleanup completed. Current stashes:"
git stash list
Recovering Accidentally Cleared Stashes
If you accidentally run git stash clear, all is not necessarily lost. Here are some recovery options:
Method 1: Check Reflog
Git maintains a reference log that can help recover lost stashes:
git reflog show stash
Look for entries that show when stashes were dropped. You might be able to recover from there.
Method 2: Use Git’s Garbage Collection
If the stashes haven’t been garbage collected yet, you might still recover them:
git fsck --unreachable --no-reflogs | grep "stash"
Method 3: Restore from Commit History
If your stashes were based on specific commits, you might be able to recreate them:
# Find the commit the stash was based on
git log --oneline --grep="WIP"
# Recreate the stash from that commit
git stash push -m "Recovered stash" -- "$(git rev-parse HEAD~1)"
Prevention for the Future
To avoid accidental stash clearing in the future:
# Create an alias with confirmation
git config --global alias.stash-clear '!f() { echo "This will delete ALL stashes. Are you sure? (y/N)" && read confirm && [ "$confirm" = "y" ] && git stash clear; }; f'
# Now use: git stash-clear
Sources
- Official Git Documentation - Stashing
- Pro Git Book - Stashing Changes
- Git Stash Clear Command Reference
- Git Reflog Documentation
- Stack Overflow - How to delete all git stashes
Conclusion
- The single command to delete all Git stashes is
git stash clear, which permanently removes all stashed changes without confirmation - Always review your stashes with
git stash listbefore clearing them to avoid losing important work - Consider backing up valuable stashes by exporting them to branches before clearing
- For selective cleanup, use
git stash dropwith specific stash references instead of clearing all at once - Create confirmation aliases or scripts to prevent accidental stash clearing in the future
- If you accidentally clear stashes, check the reflog and garbage collection for potential recovery options
Remember that git stash clear is irreversible, so use it carefully and always maintain backups of important work.