Git stash cleanup

Git stash is often used to hold temporary work that wasn’t yet ready for a commit, perhaps during a rebase. The stash history is per repo. Over time, one may accumulate numerous stash entries that are no longer relevant and may desire to cleanup this clutter.

View Git stash entries by:

git stash list

View the contents of a particular stash entry by:

git stash show -p "stash@{0}"

where the number in braces corresponds to the git stash list index.

To remove a stash entry, sliding up all the entries older than it:

git stash drop "stash@{N}"

where “N” is the entry index to remove.


If one wishes to recover a dropped Git stash entry, it may be possible:

How to recover a dropped stash in Git?

1. Find the stash commits

git log --graph --oneline --decorate ( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

This will show you all the commits at the tips of your commit graph which are no longer referenced from any branch or tag – every lost commit, including every stash commit you’ve ever created, will be somewhere in that graph.

bash/sh shell users: Version above is for Fish shell, so if you are Bash/SH user just add a $ sign before to the left parenthesis.

2. Once you know the hash of the commit you want, you can apply it as a stash

git stash apply YOUR_WIP_COMMIT_HASH_HERE

Note: The commit message will only be in this form (starting with "WIP on") if you did not supply a message when you did git stash.

Source: View the complete answer at https://stackoverflow.com/a/91795/2510591

3. If your stash commit is not listed or you don't find it (optional)

If your stash was already applied but you don't see it, for example after resolving a conflict or reset. Follow these steps:

  • Run git fsck --no-reflog | awk '/dangling commit/ {print $3}'
  • Pick a stash commit hash and use git show COMMIT_HASH in order to examine the stash commit diff of your changes.
  • After found your changes just use the corresponding commit of your stash changes and then just apply it using git stash apply COMMIT_HASH.

Bonus

If you are Fish shell user, you can take a look at GitNow which is a tool to perform faster Git operations and that can also stash your changes for you.