Git

How to Delete Stash Record in Git

Most of the time, you do not need to keep the recent changes or updates in Git that are made in the current directory while working on projects. In this situation, you can stash these changes. Git stash is a temporary shelf that saves the temporary changes. It holds changes using the indexes. Moreover, you can delete any unnecessary stash record by utilizing the “$ git stash drop” command.

In this guide, you will learn the easiest method of deleting stash records in Git.

How to Delete Stash Record in Git?

To clean the stash record, we will first make changes to the project files and add them to the Git repository. After that, execute the “$ git stash” command to hold these changes temporarily. Then, execute the “$ git stash drop” command to delete the stash records.

Let’s check out the below steps to implement the above scenario!

Step 1: Move to Git Repository
Navigate to the Git Local repository by utilizing the “cd” command:

$ cd "C:\Users\nazma\Git\mari_khan"

Step 2: Create File
Next, execute the provided command to create a new file:

$ touch file.txt

Step 3: Track File
Now, track the file using “git add” command:

$ git add file.txt

Step 4: Commit Changes
Update the Git repository and commit a related message with the “-m” option in the “git commit” command:

$ git commit -m "file.txt added"

Step 5: Update File
Open the added file on the default editor, add some text in it and update it:

$ start file.txt

Step 6: Add Changes
After updating the file, add all made changes to the Git repository with the help of provided command:

$ git add .

Step 7: Stash Changes
Now, execute the “git stash” command to temporarily hold the working directory changes:

$ git stash

Step 8: List Stash Changes
To view the recent changes, the “git stash list” command:

$ git stash list

As you can see, currently, we have only one stash with the index “0”:

Step 9: Delete Stash Record
Now, execute the stash command with “stash@{0}” to delete the related stash:

$ git stash drop stash@{0}

Below output indicates that our specific stash record is successfully deleted:

Step 10: Verify Deleted Stash
Run the “git stash” command to check any changes that need to be stashed:

$ git stash

As you can see, there are no more local changes now exist:

Want to delete a stash record using its index? Have a look at the provided procedure!

How to Delete Stash Record in Git Using Index Number?

Another command to perform the above operation in a different way is using the index number of a stash to delete.

To implement the stated concept practically, check out the following steps.

Step 1: List Stashes
Firstly, display the list of all stashes using provided command:

$ git stash list

According to the below output, currently, we have two stash indexes that contain changes:

Step 2: Remove Stash Record
Now, execute the “git stash drop” command with the index number of the stash which needs to be deleted:

$ git stash drop 1

Here, we have successfully deleted or dropped the stash record which was placed on “1” index:

How to Delete Multiple Stash Records in Git?

To delete multiple or all stashes at once, utilize the “git stash drop” command. To achieve this, follow the provided instructions.

Step 1: List Stashes
First, view the list of stashes by executing the provided command:

$ git stash list

Step 2: Delete All Stash Records
Now, simply run the “git stash drop” command to delete all stash records:

$ git stash drop

Step 3: Git Stash
Next, run the “git stash” command to check the changes that need to be stashed:

$ git stash

Below output shows that there are no changes to save:

We have illustrated different ways of deleting stashed in Git.

Conclusion

To delete the stash record in Git, first, move to the Git directory, and create and track the new text file. Then, open a file and make changes. After that, add all changes to the directory and stash the changes utilizing the “$ git stash” command. Now, execute the “$ git stash drop stash@{index}” command and delete any stash record. To delete all stashes at once, run the “$ git stash drop” command. This guide explained how to delete stash records in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.