Connect with us

Website Tutorials

How To Delete Git Stashes: Everything You Need To Know

Published

on

How To Delete Git Stashes: Everything You Need To Know

I’d earlier written about how to restore a git stash when you’re working with multiple projects. But there’s a lot more to deleting a stash that I was able to cover in that brief overview. Here’s everything you need to know about deleting a git stash. I’ll also show you how to try and recover a deleted stash, how to delete all the stashes at once, and how to filter a list of stashes so that you can delete the one you want without needing to manually sift through all the stashes.

The Difference Between Deleting and Restoring

One thing to clear up is the difference between “deleting” a stash and “popping” a stash. When you pop and restore a stash, the stash changes are applied to the current working directory, and it’s removed from the stack. This is indeed a kind of deletion, but it differs from an ordinary deletion in that the information contained in the stack is transferred elsewhere and not lost.

To pop a stack, we use the following command:

git stash pop

Here’s what the output of the above command looks like:

Git Stash Pop
Git Stash Pop

You can see that it says “Dropped refs/stash”, followed by the stash number

In the following sections, I won’t be talking about popping stashes, only permanently deleting them.

Advertisement

Dropping or Deleting Stashes

To delete the latest git stash, use the following code:

git stash drop

Here’s how it looks when I execute this in my git folder:

Git Stash Drop
Git Stash Drop

You can see that I don’t have to provide any further arguments. Git simply takes the last stash and removes it.

But if you want to remove a specific stash from the list, then you need to specify the stash number. In the below screenshot, I’ve created two stashes. Here’s what they look like along with their identification numbers:

Git Stash List
Git Stash List

You can see that my two stashes have the following IDs:

  1. stash@{0}
  2. stash@{1}

If I use the “git stash drop” command, it’ll drop the latest one, which is stash@{0}. But if I want to delete an older stash, I can specify it by name in the git drop command like this:

git stash drop stash@{0}

This will drop only the stash with the “stash@{0}” identifier.

Git Stashes are Renumbered after the Drop

It’s important to remember that once a stash is removed, the remaining stashes will be renumbered to maintain an unbroken stash ID from 0 to “n”. This means that all the stashes below the one that was dropped will move upward with new IDs to replace the one that was removed. For this reason, it’s unwise to rely on stash numbers alone, and it’s best to include a descriptive message along with the stash so that you know where any given stash stands.

Advertisement

You can do this using the following command instead of simply typing “git stash”:

git stash push -m "Second stash"

You can see that after I stash my changes with a descriptive message, I can now see the stash message whenever I use “git stash list”:

Git Stash Push
Git Stash Push

This way, you know which stash you want to delete. Remember, once you delete a stash, there’s no going back! I’ll show you how to attempt to undo mistakes, but it’s best to assume that once you drop a stash, it’s gone forever.

Deleting all Git Stashes

If you know you’re not going to use any of the previous stashes, you can simply get rid of all of them by typing:

git stash clear

This will simply wipe the entire stash stack clean. Here’s what it looks like:

Git Stash Clear
Git Stash Clear

As you can imagine, this has the potential to destroy a lot of work, so make sure you know what you’re doing before you execute it!

Searching for and Deleting Stashes

We’ve already seen that you can either “pop” or delete the latest stash using “drop”. We’ve also seen that you should be using “git stash push” to write messages before you stash something so that you know the changes you’ve made. But what if you have too many stashes to count? If you’re the kind of developer who likes to stash things on a moment’s notice, you might have built up a huge list of stashes and might want to delete one of them that contains code on which you’re no longer working.

Advertisement

Fortunately, the pipe command is very useful here. I’ve written earlier about using the pipe command in Linux. To delete a particular stash containing a certain phrase, we can perform the following steps:

  1. Get a list of all the stashes
  2. Use “grep” to filter the list based on keyword
  3. Extract the first entry
  4. Pipe the entry to the git stash drop command

Putting all this together, we get a command that looks like this:

git stash list | grep "First" | head -n 1 | sed 's/:.*//' | xargs git stash drop

The first part of the piped command gets the stash, and the second filters all the git stash entries by the keyword “First”. The third part extracts the first entry. Next, we use “sed” to extract the “stash@{x}” and the final section uses the “xargs” command to pipe it.

Note the use of sed. We need this because the output from the “head” command includes the full line of the stash, and we want to be careful that we don’t include the colon (:) when extracting the stash number. So the sed command finds the first colon and removes everything after it (including the colon itself). I’ve written earlier about how sed is useful for string manipulation, and this is a perfect example.

Instead of sed, we can also use a command like:

cut -d':' -f 1

This treats the output as a line delimited by colons and selects the first field. It’s arguably easier to read than the sed command, but both get the job done just fine.

Advertisement

You might notice that we’re not simply piping the output to the delete command, because “git stash drop” doesn’t accept arguments from stdin. This is in contrast to quite a few Linux commands like awk and sed. But the workaround is easy – just use xargs instead.

Here’s what the output of the above command looks like:

Dropping the First Stash - But not the One at the Top
Dropping the First Stash – But not the One at the Top

In the above example, you can see that I have two stashes. The one labeled “Second stash” is on top of the stack so if I just use “git stash drop”, it’ll remove that one. However, I want to remove the stash containing the word “First’. So when I run the above command, that’s exactly what happens.

Conclusion

As you can see, there are many ways to delete a git stash. The most straightforward method is to delete the most recent stash, and it has the simplest syntax. You can also delete specific stashes by specifying the ID number, and you can remove all stashes at once. Finally, I showed you how to use keyword filtering to remove stashes when you have trouble identifying the stash number.

While “popping” a stash also removes it, the information is transferred to the branch, so we can’t call it a “deletion”. I hope you found this information useful!

Advertisement

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.