Need to clear the stash list and don’t know how? Don’t worry, in this pro tip you will learn how to “throw away” stashes that you no longer need using the command git stash drop. 😉

The usual git stash workflow

Usually, you probably use git stash to store changes that are temporary and not yet ready for a commit. This often happens when you need to interrupt work to fix a bug or something similar on another branch. So your workflow should look like this:

# work work work
git stash
# switch branches and do other deliveries
# go back to the initial branch
git stash pop

So far so good, but what happens when you store changes for another reason, like cleaning up the work environment? Eventually you’ll want to get rid of these changes to keep your work environment clean, right? This is where git stash drop comes in.

Discarding a stash

Old stashes can be a source of a lot of headache, yes conflicts, I’m talking about you, so it’s important to keep your stash list up to date. Suppose you currently have the following stash list:

Image showing the stash list as output of the command git stash list with two stashes in the list

You want to get rid of stash number 0 which contains the creation of arquivo-2.txt. To do this, run the following command:

git stash drop stash@{0}

You should see a message informing the stash has been removed:

Image showing the output of the command git stash drop

Then you can double check the stash listing:

imagem mostrando a lista de stashes só com um stash como resultado de ter feito o drop de um dos stashes anteriores

It’s worth mentioning that just like git stash pop, if you don’t pass the stash name to the command git stash drop, it will remove the most recent stash from the stack.

GitFichas | GitStudyCards

Hope these commands help you to remove stashes when needed.