How to Drop a Git Stash

September 14, 2022

Introduction

Git stash allows developers to set aside their unfinished work and uncommitted files, work on something else that requires immediate attention, and later return to the stashed work. Git stores the stashes locally in a special ref until restored or deleted.

The ref can get messy if you decide to keep too many stashes, which means you have to clean it up manually.

In this tutorial, you will learn to delete and restore a deleted a Git stash.

How to drop a Git stash - a guide.

Prerequisites

How to Drop a Git Stash?

There are two ways to restore work from the stash:

  1. Using git stash pop - The command restores the changes and schedules the stash for deletion.
  2. Using git stash apply - The command restores the changes but doesn't delete the stash from the ref.

Using git stash apply too often may create a mess in the ref by storing too many stashes, which have to be manually deleted when they are no longer necessary.

The sections below show different ways of deleting a Git stash.

Note: Refer also to our article How to Restore a Git Stash for more information.

1. Drop the Most Recent Stash

Delete the most recently created Git stash by running:

git stash drop
Drop the most recently created Git stash.

The command deletes the most recent stash. However, there is no confirmation prompt or warning, so be sure you want to delete the stash before running the command.

2. Drop a Specific Stash in Git

Git stores the most recent Git stash in refs/stash, while earlier stashes are referenced by the reflog of the refs/stash reference. The reflog syntax is:

stash@{n}

{n} indicates the stash index. The most recent stash is stash@{0}, the one before that is stash@{1}, etc. To delete a specific stash, first run the following command to obtain a stash list and see which index you need to specify:

git stash list
List existing stashes in a repository.

The output contains a list of all stashes with their respective indexes. Delete a specific stash using the following syntax:

git stash drop stash@{n}

For {n}, specify the index number of the stash you want to delete. For example, to delete stash@{3}, run:

git stash drop stash@{3}
Drop a specific stash in Git.

The command deletes the specified stash and moves up the indexes of all stashes in the stack.

3. Drop All Git Stashes

When you finish working on a project and want to delete all the stashes from the ref, run the following command:

git stash clear

The command deletes all the stashes without a confirmation prompt or warning.

Recover Deleted Stash

As noted above, deleting a Git stash has no confirmation prompt or warning. If you have deleted a stash but changed your mind, there is still a way to recover it, but you need the stash commit hash.

Note: Stashes are created as Git objects whose contents aren't deleted from your machine until git-gc prunes those objects after expiration (the default is two weeks) during repository cleanup.

Follow the steps below to recover a deleted stash:

1. Obtain Stash Commit Hash

If you have restored the stash using git stash pop, the output message contains the stash commit's hash value:

Pop a Git stash.

If you deleted a stash manually, run the following command to look for the stash commit hash in the repository browser:

gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

The command opens a repository browser with every single commit in the repository ever created, reachable and unreachable:

Searching for a stash commit hash in the Git repository browser.

To find the stash commit, look for a commit message if you have specified one. If you didn't provide a message while stashing, the messages applied by default start with "WIP on …". Copy the SHA value from the SHA1 ID box.

2. Restore Deleted Stash

After finding the dropped stash commit hash, use the following syntax to restore the stash:

git stash apply [stash_hash]

For example:

Applying a deleted Git stash changes.

The command applies the stashed changes. Alternatively, apply the stashed changes to a new branch using the following syntax:

git branch [branch_name] [stash_hash]

For [branch_name], specify the name of the new branch. For example, we created a new branch called recovered-stash and switched to that branch:

Applying a deleted Git stash changes to a new Git branch.

Conclusion

This tutorial showed how to delete the most recent or specific Git stash. You also learned to recover a deleted Git stash.

For more Git tutorials, see learn to use Git, how to use Git Bash, or learn the basics of using Git stash.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Git Stash Specific Files
September 13, 2022

This tutorial shows how to stash a specific file using Git. Stashing is a great way of putting aside unfinished changes that aren't yet ready to be committed.
Read more
Git Tag: An Overview of the Basic Functions
September 6, 2022

This guide shows the basic functions to perform with Git tags. See how to create, delete, push, replace, or checkout a Git tag.
Read more
How To Pull The Latest Git Submodule
August 17, 2022

With submodules, other Git sources can be used in a project copying the code. This tutorial shows how to pull the latest Git submodule.
Read more
How to Restore a Git Repository
August 1, 2022

Deleted or overwritten a repository? Don't worry just yet, this guide covers some steps to try and restore the repository.
Read more