{"id":880,"date":"2023-05-17T05:54:26","date_gmt":"2023-05-17T05:54:26","guid":{"rendered":"https:\/\/www.dedicatedcore.com\/blog\/?p=880"},"modified":"2024-10-26T08:19:33","modified_gmt":"2024-10-26T08:19:33","slug":"drop-git-stash-recover-deleted-stash","status":"publish","type":"post","link":"https:\/\/www.dedicatedcore.com\/blog\/drop-git-stash-recover-deleted-stash\/","title":{"rendered":"How to Delete Git Stash and Restore Deleted Stash"},"content":{"rendered":"<p>Git stashing is a useful tool that enables developers to save work-in-progress. Also modifications without committing them permanently. But eventually, those hidden alterations become outmoded or are no longer required. You&#8217;ll then need to understand how to drop a Git stash or say Clear a Git Stash. This tutorial will walk you through the process of effectively deleting a Git stash. Allowing you to maintain a tidy and well-organized version control history. Configuring a username and email in Git helps <a href=\"https:\/\/www.dedicatedcore.com\/blog\/git-config-username-email\/\" target=\"_blank\" rel=\"noopener\">track a contribution to a project<\/a> to identify the author of the changes, Git commits with every username and email.<\/p>\n<p>Git stash enables developers to lay aside their uncommitted files and unfinished work. Focus on something else that needs their immediate attention. And once done then go back to the unfinished work. The stashes are kept by Git locally in a unique ref until they are destroyed or restored. If you elect to retain too many stashes, the ref may get disorganized. In which case you will need to manually clean it.<\/p>\n<h3>What is a Git Stash Drop?<\/h3>\n<p>Work from the stockpile can be restored in one of two ways:<\/p>\n<ul>\n<li>Using git stash pop, you can schedule the destruction of the stash and restore the changes.<\/li>\n<li>When using the git stash apply command, the changes are restored but the stash is left in the ref.<\/li>\n<\/ul>\n<p>The git stash applied may screw with the ref if it is used too frequently since it stores too many stashes, which must be manually erased when they are no longer required.<\/p>\n<h2>Steps For Deleting Git Stash<\/h2>\n<p>The sections below demonstrate various approaches to removing a Git stash.<\/p>\n<h3>Step 1. Remove the Newest Stash<\/h3>\n<p>Run the following command to remove the most recent Git stash:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git stash drop<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-883\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/remove-recent-git-stash.png\" alt=\"drop recently created git stash\" width=\"800\" height=\"152\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/remove-recent-git-stash.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/remove-recent-git-stash-300x57.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/remove-recent-git-stash-150x29.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/remove-recent-git-stash-768x146.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/remove-recent-git-stash-100x19.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/remove-recent-git-stash-700x133.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The most recent stash is deleted by the command. However, there is no prompt or warning before deleting the stash, so make sure you want to do it before executing the command.<\/p>\n<h3>Step 2. Drop a Particular Stash in Git<\/h3>\n<p>While prior stashes are referred to by the reflog of the refs\/stash reference, Git stores the most recent Git stash in refs\/stash. The syntax for reflog is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">stash@{n}<\/pre>\n<p>The stash index is denoted by &#8220;n.&#8221; Stash@0 refers to the most current stash, followed by Stash@1, etc. Run the following command to get a stash list and determine which index you must specify before you may delete a specific stash:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git stash list<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-884\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-delete-specific-stash.png\" alt=\"output displaying list of existing git stashes\" width=\"800\" height=\"211\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-delete-specific-stash.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-delete-specific-stash-300x79.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-delete-specific-stash-150x40.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-delete-specific-stash-768x203.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-delete-specific-stash-100x26.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-delete-specific-stash-700x185.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>A list of all stashes together with their corresponding indexes may be found in the output. The syntax to delete a given stash is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git stash drop stash@{n}<\/pre>\n<p>Enter the index number of the stash you want to remove for {n}. stash@{3} to remove this, for instance. Run the following Command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git stash drop stash@{3}<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-885\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/delete-specific-stash.png\" alt=\"drop a specific stash in git\" width=\"800\" height=\"151\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/delete-specific-stash.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/delete-specific-stash-300x57.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/delete-specific-stash-150x28.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/delete-specific-stash-768x145.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/delete-specific-stash-100x19.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/delete-specific-stash-700x132.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The command updates the indexes of every stash in the stack while deleting the given stash.<\/p>\n<h3>Step 3. Eliminate all Git Stash<\/h3>\n<p>Run the following command after you&#8217;re done with a project and want to remove all the stashes from the ref:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git stash clear<\/pre>\n<p>Without asking for approval or issuing a warning, the command deletes all of the stashes.<\/p>\n<h2>Recover Deleted Stash<\/h2>\n<p>As previously mentioned, there is no confirmation dialog or alert when deleting a Git stash. There is still a way to recover a deleted stash if you change your mind. But you&#8217;ll need the commit hash of the deleted stash.<\/p>\n<p>To restore a deleted stash, follow these steps:<\/p>\n<h3>Step 1. Obtain the Commit Hash for Stash<\/h3>\n<p>The output message includes the hash value of the stash commit if you used git stash pop to restore the stash:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-886\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-pop.png\" alt=\"output message contains stash commit hash value\" width=\"800\" height=\"311\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-pop.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-pop-300x117.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-pop-150x58.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-pop-768x299.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-pop-100x39.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-pop-700x272.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>Run the following command to search for the stash commit hash in the repository browser if you manually destroyed a stash:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">gitk --all $( git fsck --no-reflog | awk '\/dangling commit\/ {print $3}' )<\/pre>\n<p>The command launches a repository browser that displays every commit that has ever been made in the repository, both reachable and unreachable:<\/p>\n<p>If you have supplied a commit message, look for it to discover the stash commit. If you didn&#8217;t include a message while stashing, &#8220;WIP on&#8230;&#8221; will be the first line of any messages that are applied. The SHA1 ID box&#8217;s SHA value should be copied.<\/p>\n<h3>Step 2: Restoration of Deleted Stash<\/h3>\n<p>Use the following syntax to restore the stash after locating the lost stash commit hash:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git stash apply [stash_hash]<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-887\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-git-stash.png\" alt=\"syntax to restore deleted stash\" width=\"800\" height=\"270\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-git-stash.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-git-stash-300x101.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-git-stash-150x51.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-git-stash-768x259.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-git-stash-100x34.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-git-stash-700x236.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The stashed modifications are applied by the command. Alternately, use the following syntax to apply the stashed modifications to a new branch:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git branch [branch_name] [stash_hash]<\/pre>\n<p>Enter the name of the new branch as [branch_name]. As an illustration, we added a new branch called recovered-stash and switched to it:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-888\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-stash-new-branch.png\" alt=\"to new git branch apply deleted git stash changes\" width=\"800\" height=\"212\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-stash-new-branch.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-stash-new-branch-300x80.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-stash-new-branch-150x40.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-stash-new-branch-768x204.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-stash-new-branch-100x27.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/restore-stash-new-branch-700x186.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<h4>Final Wording on Deleting Git Stash and Recovering Stash<\/h4>\n<p>Dropping a Git stash may seem like a simple task. But it&#8217;s crucial to running an effective and well-organized version control system. Hope you learned how to organize your stash list and make sure that your repository history is brief. It is meaningful to follow the easy steps given in this article. Learn how to efficiently list and manage remote branches in Git to ensure your <a href=\"https:\/\/www.dedicatedcore.com\/blog\/git-list-remote-branches\/\" target=\"_blank\" rel=\"noopener\">codebase stays synchronized<\/a> and organized across collaborative projects.<\/p>\n<p>Therefore, keep these methods in mind the next time you have unneeded changes stored away to simplify your Git workflow. We definitely would be glad if this would have helped you in knowing about How to Drop a Git Stash or G<span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;git stash delete&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:14653,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;5&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:13421772}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:2}]},&quot;6&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:13421772}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:2}]},&quot;7&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:13421772}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:2}]},&quot;8&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:13421772}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:2}]},&quot;11&quot;:4,&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;,&quot;16&quot;:10}\">it Stash Delete.<\/span><\/p>\n<h3>Frequently Asked Questions (FAQ)<\/h3>\n<p><strong>1. What is Git stash?<\/strong><\/p>\n<p>This helps you to not lose your work while switching to other branches. The git stash also helps to save the changes in the working directory.<\/p>\n<p><strong>2. How do I delete a Git stash?<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git stash drop stash@{&lt;index&gt;}<\/pre>\n<p>With the help of the above command, you can delete a specific stash.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Git stashing is a useful tool that enables developers to save work-in-progress. Also modifications without committing them permanently. But eventually, those hidden alterations become outmoded&#8230;<\/p>\n","protected":false},"author":1,"featured_media":916,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/880"}],"collection":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/comments?post=880"}],"version-history":[{"count":12,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/880\/revisions"}],"predecessor-version":[{"id":2511,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/880\/revisions\/2511"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/media\/916"}],"wp:attachment":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/media?parent=880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/categories?post=880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/tags?post=880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}