<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ gitpop - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ gitpop - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 14 May 2026 20:25:01 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/gitpop/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Use Git Stash to Efficiently Manage Your Code ]]>
                </title>
                <description>
                    <![CDATA[ Sometimes when you’re coding, you need to leave a particular task incomplete to focus on another task – but you don't want to lose your progress. So what do you do? Fortunately, git stash comes to the rescue. In this article, you’ll learn all about t... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-git-stash-to-manage-code/</link>
                <guid isPermaLink="false">67092ab15a02c36736216d67</guid>
                
                    <category>
                        <![CDATA[ gitpop ]]>
                    </category>
                
                    <category>
                        <![CDATA[ gitapply ]]>
                    </category>
                
                    <category>
                        <![CDATA[ git stash ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Git ]]>
                    </category>
                
                    <category>
                        <![CDATA[ code management ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Okoro Emmanuel Nzube ]]>
                </dc:creator>
                <pubDate>Fri, 11 Oct 2024 13:40:01 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1728563160215/147e5b8d-960d-4a90-ad2f-6d71337ac0ce.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Sometimes when you’re coding, you need to leave a particular task incomplete to focus on another task – but you don't want to lose your progress. So what do you do? Fortunately, <code>git stash</code> comes to the rescue.</p>
<p>In this article, you’ll learn all about the <a target="_blank" href="https://git-scm.com/docs/git-stash">git stash</a> command and why it’s important to stash your code. By the end of this article, you will have firsthand knowledge of how to use the <code>git stash</code> command in your projects.</p>
<h3 id="heading-heres-what-well-cover">Here’s what we’ll cover:</h3>
<ol>
<li><p><a class="post-section-overview" href="#heading-why-do-we-stash">Why do we stash?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-pros-and-cons-of-using-git-stash">Pros and cons of using git stash</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-create-a-stash-in-git-step-by-step-guide">How to create a stash in Git (step-by-step guide)</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-other-git-stash-commands-like-list-apply-and-pop">Other git stash commands like list, apply, and pop</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-handle-multiple-stashes-in-your-project">How to handle multiple stashes in your project</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-real-use-cases-for-git-stash">Real use cases for git stash</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-why-do-we-stash">Why Do We Stash?</h2>
<p><a target="_blank" href="https://git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F">Git</a> has provided us with the stash command, which makes our lives easier. The <code>git stash</code> command helps you save a draft of your current task, temporarily giving you time to attend to your new task without losing your progress on the previous task.</p>
<p>There are so many reasons why stashing is important. Here are some of them:</p>
<ul>
<li><p><strong>Resolving Conflicts within a Project</strong>: In a collaboration setting, you, as a developer, get to work with other developers on a particular project. A merge conflict might occur, which might require you to pause your current task to handle the conflict. You can easily stash your current task and focus fully on resolving the merge conflict without having to worry about losing your progress.</p>
</li>
<li><p><strong>Clean Working Environment</strong>: You might want to start up a new task or want to pull a repository into your working environment. With stashing, you can clear your current working environment temporarily, making your work environment clean and ready to perform another task.</p>
</li>
<li><p><strong>Switching branches</strong>: <code>Git stash</code> is very useful in this situation. You might be in the middle of something and it's not ready to be committed, but at that moment, you need to switch branches. With <code>git stash</code>, you can easily move to another branch and perform your other tasks.</p>
</li>
</ul>
<h2 id="heading-pros-and-cons-of-stashing">Pros and Cons of Stashing</h2>
<p>Here are some of the pros (advantages) of using stash:</p>
<ul>
<li><p>It’s easy to use and understand</p>
</li>
<li><p>It helps you save a draft of your current task and focus on another task.</p>
</li>
<li><p>It comes in handy when trying to resolve conflicts like merge conflicts, <code>git fork</code>, and so on when working with other collaborators in a project.</p>
</li>
<li><p>You can reapply your draft file not only on the branch you stashed it from but also on other branches.</p>
</li>
</ul>
<p>As the saying goes "Anything that has an advantage also has a disadvantage". Here are some of the cons (disadvantages) of using stash:</p>
<ul>
<li><p>Stashing can lead to a merge conflict: A merge conflict can occur when reapplying your stashed draft to a branch which already has similar content to the draft, and you’ll need to resolve this manually.</p>
</li>
<li><p>Cluster and Confusion: In a situation where you are working on a huge project with multiple sub-tasks, applying the stash command at various points might lead to a cluster of saved drafts. This can lead to confusion on the particular saved draft you would want to continue working on.</p>
</li>
</ul>
<p>Now that we have seen some of the pros and cons of using <code>git stash</code>, let’s look at how <code>git stash</code> works and how to apply it to our project.</p>
<h2 id="heading-how-git-stash-works">How Git Stash Works</h2>
<p>As we just discussed, <code>git stash</code> helps you save a draft of your current uncommitted changes. Now let's see how this happens using the analogy below.</p>
<p>When you run the <code>git stash</code> command, it puts your tracked file in a box and then removes/hides that box, making the environment/branch clean and free to use for another task. When you apply the stash command to the current branch you are working on, it saves a draft of all tracked files in that branch and reverts that branch to a clean slate.</p>
<p>To explain better, here’s an example:</p>
<p><img src="https://hackmd.io/_uploads/S1x5zSPhR.gif" alt="Image of How Git Stash Works" width="600" height="400" loading="lazy"></p>
<p>From the image above, we created a new project and already initialized <code>git</code> in it. We are currently on the "main" branch which is the default branch.</p>
<p>If we modify a file in the <code>main</code> branch and apply the <code>git stash</code> command, the modified file will be saved as a draft and your working environment will be reverted to the last commit you made (making it look like there was no modification in the first place).</p>
<p>Note: By default, you can only apply the stash command to tracked files in <code>git</code>.</p>
<h3 id="heading-how-to-create-a-stash">How to Create a Stash</h3>
<p>Here is how you can go about creating a stash in your project.</p>
<p>Create a file you want to work on, maybe an <code>index.html</code> file or a <code>style.css</code> file. Initialize <code>git</code> in your project by running the command <code>git init</code>. Add the file to the Git tracking stage by running <code>git add .</code> Then at this point, if you make any modifications and would like to come back later to complete it, you can run:</p>
<pre><code class="lang-bash">git stash
</code></pre>
<p>Here is a visual representation of the above process:</p>
<p><img src="https://hackmd.io/_uploads/HJg8cHDh0.gif" alt="Visual Representation of How to Create a stash" width="600" height="400" loading="lazy"></p>
<p>There are other stash commands you should know in order to control your stashed project. They include <a target="_blank" href="https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-listltlog-optionsgt">List</a>, <a target="_blank" href="https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-pop--index-q--quietltstashgt">Pop</a>, <a target="_blank" href="https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-clear">Clear</a>, <a target="_blank" href="https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-apply--index-q--quietltstashgt">Apply</a>, <a target="_blank" href="https://git-scm.com/docs/git-stash#Documentation/git-stash.txt-show-u--include-untracked--only-untrackedltdiff-optionsgtltstashgt">Show</a>, and a few others.</p>
<ul>
<li><p><strong>List</strong>: The list command is mainly used to display the number of stashes made in a particular project. Just like an array, the stashed draft is numbered from 0 upwards. To use this command, run <code>git stash list</code>. Note: The stash command is arranged in such a way that when a change is stashed, it takes up the first position i.e."stash@{0}" while the other changes are pushed down.</p>
</li>
<li><p><strong>Pop</strong>: This command is used to retrieve an already stashed draft back into your project. When you apply the pop command, it automatically retrieves the latest stashed draft back into your working project and removes it from the stash list. Run the following command to pop a stash: <code>git stash pop</code>.</p>
</li>
<li><p><strong>Clear</strong>: When applied, this command is used to remove/delete all the stash entries. It is important to note that, when you use the clear command, you can recover the cleared stash again. You can use it by running this command: <code>git stash clear</code>.</p>
</li>
<li><p><strong>Apply</strong>: This command works just like the pop command, but the only difference is that the stashed draft is not removed from the list after it has been retrieved. This means that when you use the apply command, it retrieves the latest stashed draft back into your project but doesn't remove it from the stash list. Run the following to use the apply command: <code>git stash apply</code>.</p>
</li>
<li><p><strong>Show</strong>: This command is important because it helps show you the changes you made in the stashed draft. Use the following command to show a stash: <code>git stash show</code>.</p>
</li>
</ul>
<h2 id="heading-how-to-handle-multiple-stashes-in-your-project">How to Handle Multiple Stashes in Your Project</h2>
<p>Knowing how to handle multiple stashes in your project is important, as there may be times when you have more than 3-4 stashed drafts in your project.</p>
<p>Here is how to handle multiple stashes:</p>
<h3 id="heading-step-one-customize-each-stash-with-a-specific-message">Step One: Customize each stash with a specific message</h3>
<p>This step is very important if you would like to avoid being confused in situations where you have multiple stashes.</p>
<p><img src="https://hackmd.io/_uploads/BkVtICNIA.png" alt="Multiple stashes with the Same messages" width="600" height="400" loading="lazy"></p>
<p>From the image above, we have a total of four stashes (that is, "stash@{0}" to "stash@{3)") and each stash bears the same message of "first commit". This might be confusing when we might want to pop or apply one of these stashes later in the future.</p>
<p>To sort this issue, all we have to do is assign a specific stash message to the next stash. We can do this by running the following command:</p>
<pre><code class="lang-bash">git stash save <span class="hljs-string">"New message/name goes here"</span>
</code></pre>
<p>Here is how it looks:</p>
<p><img src="https://hackmd.io/_uploads/HJH3IRNLA.png" alt="Multiple stashes with the different messages" width="600" height="400" loading="lazy"></p>
<p>From the image above, you can clearly see the difference between the two latest stashes (that is, "stash@{0}" and "stash@{1}") and the others. The first two stashes have their own specific message, which makes it very easy to differentiate them from the rest.</p>
<h3 id="heading-step-two-retrieve-a-specific-stash-instead-of-the-latest-stash">Step Two: Retrieve a specific stash instead of the latest stash</h3>
<p>There are situations when you might want to retrieve a specific stashed draft rather than the latest stashed draft you just made. You can do this by using either:</p>
<pre><code class="lang-bash">git stash pop stash@{n}
</code></pre>
<p>or</p>
<pre><code class="lang-bash">git stash apply stash@{n}
</code></pre>
<p>Where <code>n</code> indicates the stash ID you want to retrieve. This also works when you want to delete or clear a specific stashed draft.</p>
<h3 id="heading-step-three-preview-your-stashed-draft-before-you-retrieve-it">Step Three: Preview your stashed draft before you retrieve it.</h3>
<p>Before you restore a stashed draft, it is important to preview it to ensure that it's the exact draft you want to restore. The <code>show</code> command helps you review the changes in the draft before restoring it. To do this, run the following command:</p>
<pre><code class="lang-bash">git stash show stash@{n}
</code></pre>
<h2 id="heading-real-use-cases-for-git-stash">Real Use Cases for <code>git stash</code></h2>
<p>Here are some real-life case scenarios of when the git stash command is important.</p>
<ul>
<li><p><strong>During Debugging</strong>: This happens a lot when you are in a collaboration setting. Let's say you are working with extra two developers on a project and one of them happens to encounter an error that needs your urgent attention. Using the <code>git stash</code> command is ideal in this situation.</p>
</li>
<li><p><strong>When you are not ready to Commit</strong>: This occurs often. There are situations where you are not ready to commit your changes to the repository. Stashing those changes is the best next step.</p>
</li>
<li><p><strong>Returning the directory to its original state</strong>: Returning the directory to its original state simply means cleaning out all changes made to the directory and making it look as if nothing was done to it. The <code>git stash</code> command helps you achieve this.</p>
</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>The <code>git stash</code> command effortlessly helps you manage your project properly whether you are working alone or you’re in a collaboration setting.</p>
<p>This tool is vital to every developer who wants to flow freely and effortlessly while working on a project.</p>
<p>At this point, I believe you know what <code>git stash</code> is all about, why it is important and how to make use of it in your project.</p>
<p>Thank you for reading.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
