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.
Prerequisites
- Git installed (install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
How to Drop a Git Stash?
There are two ways to restore work from the stash:
- Using
git stash pop
– The command restores the changes and schedules the stash for deletion. - 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
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
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}
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:
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:
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:
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:
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.