laur
laur

Reputation: 590

How to delete stash by commit sha

Is it possible to delete stash by its commit sha instead of dropping by the index using

git stash drop stash@{index}

Upvotes: 1

Views: 173

Answers (1)

torek
torek

Reputation: 488193

Not directly, no. You will have to convert the hash to its corresponding reflog entry, and drop it by reflog-entry-name.

Use git reflog stash or its equivalent (but more malleable) git log -g stash to walk the stash reflog looking for the commit by ID. Consider what to do if it is not present (perhaps the stash was already dropped), and/or what to do if it occurs more than once (this should never happen in normal operation, but there's nothing fundamentally preventing the stash reflog from listing the same commit hash several times).

git log -g --format='%H %gd' stash | \
    awk -v h=$hash '$1 == h { print $2 }'

will print one line per matching hash (assuming $hash is set to the full 40-character hash).

Upvotes: 2

Related Questions