{"id":1757,"date":"2022-01-02T16:17:32","date_gmt":"2022-01-02T22:17:32","guid":{"rendered":"https:\/\/www.becomebetterprogrammer.com\/?p=1757"},"modified":"2022-04-25T09:37:16","modified_gmt":"2022-04-25T14:37:16","slug":"git-stash-with-name","status":"publish","type":"post","link":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/","title":{"rendered":"How to Use Git Stash: Name and Retrieve Stashed Changes"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">It is common for software developers to multitask and constantly shift their focus to working on something different. An example of this looks like this: someone started working on a &#8220;feature&#8221; branch, but all of a sudden changed to the &#8220;master&#8221; branch after discovering a bug in production. Having code changes in the &#8220;feature&#8221; branch, and changing to the &#8220;master&#8221; branch can be messy. Fortunately, there is a solution for this: git stash.   <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Git stash is a powerful tool to stash or secretly save changes made on any tracked files without the need to commit them, which allows retrieving those changes at any point in time regardless of the current branch in use<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Learning another command means adding more to the list of things developers need to remember. Fortunately, git stash is not complex to learn. In this article, you are going to learn everything you need to know about git stash: how to create, list, apply, drop, and remove stashed changes.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#How_to_use_Git_Stash_Creating_a_Stash\" >How to use Git Stash: Creating a Stash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#List_Git_Stashes\" >List Git Stashes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Apply_Stashed_Changes\" >Apply Stashed Changes<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Applying_a_specific_stash_version\" >Applying a specific stash version<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Applying_a_stash_using_pop_option\" >Applying a stash using pop option<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Difference_Between_Pop_and_Apply_Git_Stashes\" >Difference Between Pop and Apply Git Stashes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Applying_and_removing_a_specific_stash_version\" >Applying and removing a specific stash version<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Removing_Git_Stashes\" >Removing Git Stashes<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Removing_All_Git_Stashes\" >Removing All Git Stashes<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Name_and_Retrieve_Git_Stashes_by_Name\" >Name and Retrieve Git Stashes by Name<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Adding_a_name_to_a_git_stash\" >Adding a name to a git stash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Retrieve_a_git_stash_by_name\" >Retrieve a git stash by name<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Using_Regex\" >Using Regex<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Using_stash_index\" >Using stash index<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/git-stash-with-name\/#Interested_in_Learning_more_about_Git\" >Interested in Learning more about Git?<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_use_Git_Stash_Creating_a_Stash\"><\/span>How to use Git Stash: Creating a Stash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To create a stash, use the <code>git stash<\/code> command once you know there are unfinished changes you don&#8217;t want to commit. For the purposes of demonstrating how to use <code>git stash<\/code>, we are going to look at the following example where initially there are no changes in index.html and styles.css files.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"369\" height=\"93\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/Unmodified-Files.png\" alt=\"\" class=\"wp-image-1759\" title=\"Unmodified Files\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Unmodified-Files.png 369w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Unmodified-Files-300x76.png 300w\" sizes=\"auto, (max-width: 369px) 100vw, 369px\" \/><figcaption>Unmodified Files<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">At any point, we decide to add an index.css file as well as make changes to the index.html.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"371\" height=\"126\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/Modified-Files.png\" alt=\"\" class=\"wp-image-1760\" title=\"Added index.css file and modified index.html file\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Modified-Files.png 371w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Modified-Files-300x102.png 300w\" sizes=\"auto, (max-width: 371px) 100vw, 371px\" \/><figcaption>Added index.css file and modified index.html file<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, if we verify the status of the current branch with <code>git status<\/code>, git will say which files are untracked and which files are staged and unstaged.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git status\nOn branch master\nYour branch is up to date with 'origin\/master'.\n\nChanges not staged for commit:\n  (use \"git add &lt;file&gt;...\" to update what will be committed)\n  (use \"git restore &lt;file&gt;...\" to discard changes in working directory)\n        modified:   index.html\n\nUntracked files:\n  (use \"git add &lt;file&gt;...\" to include in what will be committed) \n        index.css\n\nno changes added to commit (use \"git add\" and\/or \"git commit -a\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If for any reason we need to stash the following changes, use the <code>stash<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Running the <code>stash<\/code> command will only stash changes made to the index.html file, and revert it back to the way it initially was as if no changes were made.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"356\" height=\"118\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/Only-index.html-changes-were-stashed.png\" alt=\"\" class=\"wp-image-1761\" title=\"Only index.html changes were stashed\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Only-index.html-changes-were-stashed.png 356w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Only-index.html-changes-were-stashed-300x99.png 300w\" sizes=\"auto, (max-width: 356px) 100vw, 356px\" \/><figcaption>Only index.html changes were stashed<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">However, the index.css file was not stashed. Otherwise, we should have seen our repository in the way we initially had it without any changes. The reason is <code>git stash<\/code> <strong>will only stash modified tracked files<\/strong>. Hence, index.html and styles.css are tracked files, and index.css is untracked unless it is staged.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we stage the index.css, the index.css file becomes a tracked file in git. Once staged, we can stash any changes made to the index.css file. To see this, stage the index.css file using the <code>add<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git add index.css<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, verify index.css was actually staged.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git status\nOn branch master\nYour branch is up to date with 'origin\/master'.\n\nChanges to be committed:\n  (use \"git restore --staged &lt;file&gt;...\" to unstage)\n        new file:   index.css\n\nChanges not staged for commit:\n  (use \"git add &lt;file&gt;...\" to update what will be committed)\n  (use \"git restore &lt;file&gt;...\" to discard changes in working directory)\n        modified:   index.html<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We verify index.css file is staged by seeing if there is a message displayed below &#8220;Changes to be committed:&#8221; text. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since the index.css file was successfully staged, now we can stash it.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Notice how the index.css file &#8220;disappears&#8221; from the root folder of the repository, which means it has correctly stashed.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"369\" height=\"93\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/Index.css-dissappeared-after-staging-and-stashing.png\" alt=\"\" class=\"wp-image-1762\" title=\"Index.css disappeared after staging and stashing\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Index.css-dissappeared-after-staging-and-stashing.png 369w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Index.css-dissappeared-after-staging-and-stashing-300x76.png 300w\" sizes=\"auto, (max-width: 369px) 100vw, 369px\" \/><figcaption>Index.css disappeared after staging and stashing<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"List_Git_Stashes\"><\/span>List Git Stashes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To see the list of git stashes, add the <code>list<\/code> option after the <code>stash<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash list<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The terminal will display all of the stashed changes made in the repository.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"205\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/List-of-git-stashes.png\" alt=\"\" class=\"wp-image-1766\" title=\"List of Git Stashes\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/List-of-git-stashes.png 680w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/List-of-git-stashes-300x90.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><figcaption>List of Git Stashes<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Every time you stash changes, git tracks the stashes in a stack.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"500\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/Stack-of-Git-Stashes.png\" alt=\"\" class=\"wp-image-1765\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Stack-of-Git-Stashes.png 800w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Stack-of-Git-Stashes-300x188.png 300w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Stack-of-Git-Stashes-768x480.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore, the latest stash is moved to the top of the stack which is identified with the index 0 (<code>stash@{0}<\/code>). Storing the stashes in a stack plays a key role when using other functionality when using <code>apply<\/code> and <code>pop<\/code> options to retrieve a stash. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Apply_Stashed_Changes\"><\/span>Apply Stashed Changes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Learning how to stash changes wouldn&#8217;t be complete if there is no way to apply stashed changes. To apply stashed changes, use the option <code>apply<\/code> to use the last stash (<code>stash@{0}<\/code>) in the stack of git stashes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash apply<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the latest stashed changes we had were for adding an index.css file and making changes to the index.html file, those changes will be readded.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/Git-stash-apply-before-and-after.png\" alt=\"\" class=\"wp-image-1767\" width=\"800\" height=\"500\" title=\"Before and after applying stashed changes\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Git-stash-apply-before-and-after.png 800w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Git-stash-apply-before-and-after-300x188.png 300w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Git-stash-apply-before-and-after-768x480.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><figcaption>Before and after applying stashed changes<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Applying_a_specific_stash_version\"><\/span>Applying a specific stash version<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It is possible to apply a specific stash version. For instance, if after executing <code>git stash list<\/code> , there is a list of git stashes like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"205\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/List-of-git-stashes.png\" alt=\"\" class=\"wp-image-1766\" title=\"List of Git Stashes\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/List-of-git-stashes.png 680w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/List-of-git-stashes-300x90.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><figcaption>List of Git Stashes<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">And you need to apply the stash that says: <code>stash@{6}: WIP on master: eeed257 Revert \"worked on navigation panel\"<\/code>, add the stash index number after using the <code>apply<\/code> command. Therefore, use the following command to apply <code>stash@{6}<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash apply \"stash@{6}\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Applying_a_stash_using_pop_option\"><\/span>Applying a stash using <code>pop<\/code> option<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It is possible to apply stashed changes using the <code>pop<\/code> option. Similar to using <code>apply<\/code> option without defining a specific stash version, it will apply the last stash in stack or <code>stash@{0}<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash pop<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Difference_Between_Pop_and_Apply_Git_Stashes\"><\/span>Difference Between Pop and Apply Git Stashes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The difference between <code>git stash apply<\/code> and <code>git stash pop<\/code> is <code>apply<\/code> option only applies the stash while the <code>pop<\/code> option applies the stash but also removes the stash from the stack.<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/Git-stash-apply-vs-pop.png\" alt=\"\" class=\"wp-image-1769\" width=\"762\" height=\"476\" title=\"Difference between git stash apply vs git stash pop\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Git-stash-apply-vs-pop.png 800w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Git-stash-apply-vs-pop-300x188.png 300w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Git-stash-apply-vs-pop-768x480.png 768w\" sizes=\"auto, (max-width: 762px) 100vw, 762px\" \/><figcaption>Difference between <code>git stash apply<\/code> vs <code>git stash pop<\/code><\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Notice how we previously had 8 git stashes, but after using <code>git stash pop<\/code>, we have now 7 git stashes.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"182\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/After-applying-stash-pop.png\" alt=\"\" class=\"wp-image-1768\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/After-applying-stash-pop.png 782w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/After-applying-stash-pop-300x70.png 300w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/After-applying-stash-pop-768x179.png 768w\" sizes=\"auto, (max-width: 782px) 100vw, 782px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Applying_and_removing_a_specific_stash_version\"><\/span>Applying and removing a specific stash version<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To use <code>pop<\/code> on a specific stash version in a similar way it is used when using the <code>apply<\/code> option. Make sure to provide the specific stash version. The following example is the command used If we want to apply and delete the stash: <code>stash@{5}: WIP on master: eeed257 Revert \"worked on navigation panel\"<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash pop \"stash@{5}\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Notice how the stash <code>WIP on master: eeed257 Revert \"worked on navigation panel<\/code> is removed after checking the list of git stashes using <code>git stash list<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"165\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/After-applying-a-specific-stash-pop-to-stash@5.png\" alt=\"\" class=\"wp-image-1770\" title=\"Checking the list of git stashes after git stash pop &quot;stash@{5}&quot;\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/After-applying-a-specific-stash-pop-to-stash@5.png 601w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/After-applying-a-specific-stash-pop-to-stash@5-300x82.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><figcaption>Checking the list of git stashes after <code>git stash pop \"stash@{5}\"<\/code><\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Removing_Git_Stashes\"><\/span>Removing Git Stashes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes developers keep adding git stashes and applying them later on using the <code>apply<\/code> option without necessarily removing them. To remove a git stash, use the <code>drop<\/code> option. When using the drop option without specifying the stashed version, it will remove the last git stash (<code>stash@{0}<\/code>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash drop<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To remove a specific stash version, provide the stash version when using the <code>drop<\/code> option.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash drop \"stash@{3}\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Git stash <code>drop<\/code> option differs from <code>pop<\/code> option as it does not apply the stashed changes.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Removing_All_Git_Stashes\"><\/span>Removing All Git Stashes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In case of wanting to remove all git stashes, it is possible to use them over and over <code>git stash drop<\/code> until all stashes are deleted. However, to remove them all at the same time use the <code>clear<\/code> option.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash clear<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Name_and_Retrieve_Git_Stashes_by_Name\"><\/span>Name and Retrieve Git Stashes by Name<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Adding_a_name_to_a_git_stash\"><\/span>Adding a name to a git stash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you understand how to create, apply, and remove git stashes, we are going to cover how to add stash with a name. If you noticed in the previous examples, git generates automatically a name or a message per each stash added. Sometimes these generated messages are the same when adding multiple stashes, which makes it hard to identify the stashed changes.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"165\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/After-applying-a-specific-stash-pop-to-stash@5.png\" alt=\"\" class=\"wp-image-1770\" title=\"Git generates a message per each stash\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/After-applying-a-specific-stash-pop-to-stash@5.png 601w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/After-applying-a-specific-stash-pop-to-stash@5-300x82.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><figcaption>Git generates a message per each stash<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Notice how the message: <code>\"worked on navigation panel is removed\"<\/code> is used five of the six stashes in the previous image. To better identify a git stash, add a custom name or message by using the option <code>-m<\/code> followed by the message when creating a stash.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash -m removing_styles<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can quickly verify the message was added to the stash by listing the git stashes using the <code>git stash list<\/code> command.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"491\" height=\"49\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/Adding-a-custom-name-to-stash-1.png\" alt=\"\" class=\"wp-image-1772\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Adding-a-custom-name-to-stash-1.png 491w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Adding-a-custom-name-to-stash-1-300x30.png 300w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><figcaption>Custom message added to a stash<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Retrieve_a_git_stash_by_name\"><\/span>Retrieve a git stash by name<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_Regex\"><\/span>Using Regex<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One way to retrieve and apply a stash based on a given name is using regex when using <code>apply<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash apply stash^{\/removing_styles}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_stash_index\"><\/span>Using stash index<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Another approach if you are not familiar with using regex is to display the list of git stashes and find out the index of the stash with the custom name.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"449\" height=\"89\" src=\"https:\/\/www.becomebetterprogrammer.com\/wp-content\/uploads\/2022\/01\/Finding-stash-with-custom-name.png\" alt=\"\" class=\"wp-image-1773\" title=\"Finding stash index with the custom name\" srcset=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Finding-stash-with-custom-name.png 449w, https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-content\/uploads\/2022\/01\/Finding-stash-with-custom-name-300x59.png 300w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><figcaption>Finding stash index with the custom name<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Once we find the stash with the custom name, in this case <code>stash@{2}: On master: removing_styles<\/code>, we can now apply the stash based on the version.<\/p>\n\n\n\n<pre class=\"wp-block-code language-git\"><code>$ git stash apply stash@{2}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">All in all, git stash is a useful tool every software developer should include as part of their git knowledge to quickly save changes without the need of committing them in case of needing to switch branches or attempting other logic. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is possible to provide custom and more meaningful names to git stashes to easily identify git stashes whenever someone needs to verify the list of available stashes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Interested_in_Learning_more_about_Git\"><\/span>Interested in Learning more about Git?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I wrote other articles explaining how to use other git commands, and I thought you might be interested in reading some of them since you are reading this.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.becomebetterprogrammer.com\/git-alias\/\">The Complete Guide to Git Alias: Shorcuts to Be Efficient<\/a><\/li><li><a href=\"https:\/\/www.becomebetterprogrammer.com\/git-rebase\/\" title=\"https:\/\/www.becomebetterprogrammer.com\/git-rebase\/\">How to Rebase in Git: Explained Step-by-Step<\/a><\/li><li><a href=\"https:\/\/www.becomebetterprogrammer.com\/git-revert-last-commit\/\">How to Revert the Last Commit Locally and Remote in Git<\/a><\/li><li><a href=\"https:\/\/www.becomebetterprogrammer.com\/git-head\/\">What is Git HEAD? A Practical Guide Explained with Examples<\/a><\/li><li><a href=\"https:\/\/www.becomebetterprogrammer.com\/learn-how-to-use-version-control-with-git-and-github-the-absolute-guide-for-beginners\/\">Learn How to Use Version Control with Git and GitHub: The Absolute Guide for Beginners<\/a><\/li><li><a href=\"https:\/\/www.becomebetterprogrammer.com\/how-to-update-github-personal-access-tokens\/\">How to Update GitHub Personal Access Tokens?<\/a><\/li><\/ul>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\"><strong>Did you like this article?<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\">Share your thoughts by replying on <a href=\"https:\/\/twitter.com\/bbprogrammer\" target=\"_blank\" rel=\"noreferrer noopener\">Twitter of Become A Better Programmer<\/a> or to <a href=\"https:\/\/twitter.com\/arealesramirez\" target=\"_blank\" rel=\"noreferrer noopener\">personal my Twitter account<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-rich is-provider-twitter wp-block-embed-twitter\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"twitter-tweet\" data-width=\"550\" data-dnt=\"true\"><p lang=\"en\" dir=\"ltr\">It is 2022 and you haven&#39;t heard of git stash?<br><br>Maybe you are still enjoying your holidays, but we are starting right away this year with a new guide to help you level up your Git skills.<br><br>Check it out!<a href=\"https:\/\/t.co\/qquaVaevbx\">https:\/\/t.co\/qquaVaevbx<\/a><\/p>&mdash; Become A Better Programmer (@bbprogrammer) <a href=\"https:\/\/twitter.com\/bbprogrammer\/status\/1477766685289549830?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">January 2, 2022<\/a><\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Git stash is a tool to stash or save changes made on any tracked files without having to commit them. In this article, you will learn how to use git stash.<\/p>\n","protected":false},"author":1,"featured_media":1801,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1757","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general-info","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/1757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/comments?post=1757"}],"version-history":[{"count":5,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/1757\/revisions"}],"predecessor-version":[{"id":2468,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/1757\/revisions\/2468"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/media\/1801"}],"wp:attachment":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/media?parent=1757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/categories?post=1757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/tags?post=1757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}