Git stashes allow saving uncommitted changes to revisit later. However, these changes can pile up and should be cleared out periodically. This guide provides a comprehensive reference for deleting all git stashes.

What is the Stash List?

The stash list stacks a series of stashed changes from your working directory. These are stored in .git/refs/stash.

View your stash list:

git stash list

Output shows the stash stack with the most recent first:

stash@{0}: WIP on new-feature
stash@{1}: Untracked files  
stash@{2}: Revert changes 

The stash stack allows quick access to prior working states, enabling easy context switching. However, too many stashes makes it unwieldy.

When to Delete All Stashes

Reasons you may want to delete all git stashes:

  • Start fresh when switching tasks – Clear out outdated changes
  • Consolidate before rebasing – Avoid merge conflicts
  • Clean up old temporary stashes – No longer needed
  • Remove clutter – Too many stashes becomes disorganized

Deleting all stashes can be safer than dropping individual ones, which may become outdated.

Delete All Git Stashes

Use git stash clear:

git stash clear

This deletes all stashed changes, emptying your stash list:

# Before
git stash list
stash@{0}: WIP feature
stash@{1}: Fix header padding

# Delete all
git stash clear 

# After 
git stash list
# empty - all stashes deleted

The clear command provides a clean sweep of all stashed changes.

Alternative Deletion Methods

There are a couple other approaches to deleting multiple stashes:

Drop Each Stash

You can drop stashes one-by-one instead of clearing all:

git stash list
git stash drop stash@{0} # Drops most recent
git stash drop stash@{1} # Drops next

Dropping gives more control than clearing, but can be slow if you have many stashes.

Delete Ref File

The .git/refs/stash file stores the underlying stash data. Deleting this removes all traces of stashes:

rm -rf .git/refs/stash

This completely deletes all stash data outside Git‘s control, so use with extreme caution!.

Managing & Preventing Stash Buildup

While periodically clearing stashes can simplify workflows, providing good stash hygiene through other means is ideal.

Automate Removing Old Stashes

Manually monitoring your stashes can be challenging. Instead consider configuring stale-cleanup to auto-remove stashes older than a specified date:

git config staleclean.cleanAfter 30 # days 
git config staleclean.enabled true

This automatically cleans up old stashes in the background, while keeping only recent ones.

Apply Stashes Whenever Possible

Stashes often pile up when engineers apply changes inconsistently:

# Dev A 
➜ changes; git stash

# Dev B
➜ git pull origin; ➜ forgot to git stash apply!

Get in the habit of consistently reapplying stashes when switching tasks rather than letting them linger.

Use Git Worktrees

Rather than stashing, create git worktrees to isolate different lines of work:

git worktree add -b bug-123-fix ../temp-bug-123-fix

Worktrees provide full repositories to test changes in without impacting your main branch.

Recovering a Deleted Stash

If you mistakenly deleted a needed stash, recovery is still possible if commits haven‘t yet expired from the reflog:

# Filter by ‘stash‘
git reflog --stash

# Pick ID, ie abc123  
git show stash@{abc123} # Show changes
git stash apply abc123 # Apply from ID

However, this relies on the local reflog, which expires after 90 days by default. So restore important stashes quickly!

Stash Command Options

Here is a reference guide to key git stash subcommands:

Command Description
git stash push Adds current changes to stash stack
git stash pop Applies most recent stash and removes from stack
git stash apply Applies changes but keeps stash
git stash show Shows a summary diff of stash changes
git stash list Displays all stashes on the stack
git stash clear Deletes all stashes, emptying list

These allow flexible management such as previewing specific stashes before applying.

Conclusion

  • git stash clear deletes all stashed changes
  • Alternate methods are dropping or deleting the ref/stash file
  • Automate cleanup rather than manually clearing out stashes
  • Be extremely careful when force deleting the underlying stash data file
  • Review what the stashes contain first before blindly removing them all

I hope these guidelines provide a deep understanding of how to effectively delete and manage git stashes to improve your programming workflow! Please reach out with any other questions.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *