<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mwafrika Josué</title>
    <description>The latest articles on DEV Community by Mwafrika Josué (@mwafrika).</description>
    <link>https://dev.to/mwafrika</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F189722%2F76617080-de4b-4dbc-bc22-4fbdac1f4ed1.jpg</url>
      <title>DEV Community: Mwafrika Josué</title>
      <link>https://dev.to/mwafrika</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mwafrika"/>
    <language>en</language>
    <item>
      <title>How to Use Git Stash Command</title>
      <dc:creator>Mwafrika Josué</dc:creator>
      <pubDate>Tue, 30 Aug 2022 10:49:00 +0000</pubDate>
      <link>https://dev.to/mwafrika/how-to-use-git-stash-command-22bk</link>
      <guid>https://dev.to/mwafrika/how-to-use-git-stash-command-22bk</guid>
      <description>&lt;h2&gt;
  
  
  10 Tips you should know about Git Stash
&lt;/h2&gt;

&lt;p&gt;Assume you are working on a complex feature that demands concentration, and your boss instructs you to solve an urgent problem, and you just noticed that the final change for that feature was implemented in a different branch.&lt;/p&gt;

&lt;p&gt;In such a case, the first thought that comes to mind is to checkout to the branch that has the implemented features.&lt;/p&gt;

&lt;p&gt;If you try to checkout to another branch from unstaged to staged modifications, you can run into the following issue:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DY5aCmJA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2ko6ozoe2hxv0fc590b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DY5aCmJA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2ko6ozoe2hxv0fc590b.png" alt="Image description" width="662" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What happened in the above screenshot ?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this case, the current branch's staged and unstaged modifications will be applied to the branch you switched to.&lt;/p&gt;

&lt;p&gt;Next, Git does not always enable you to swap branches without first committing your changes. This is due to the possibility of losing the modifications you made in your current branch or having them clash with the destination branch. For whatever reason, we can't move branches without first committing or stashing the modifications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kv-HnJVv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mq51rfx8h4lty6vnwndn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kv-HnJVv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mq51rfx8h4lty6vnwndn.png" alt="Image description" width="753" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To address the aforementioned issue, we must stash the modifications in our working environment. Stashing means to store changes safely in a hidden place (the stash stack) for later use.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to stash your changes ?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Git stash&lt;/code&gt; saves the uncommitted changes locally, allowing you to make changes, switch branches, and perform other Git operations&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`git stash`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to stash untracked files ?
&lt;/h2&gt;

&lt;p&gt;You can use additional options to let git stash take care of untracked and ignored files.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To stash untracked files:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`git stash -u`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`git stash --include-untracked`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To stash untracked files and ignored files:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`git stash -a`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`git stash --all` 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to list stashes ?
&lt;/h2&gt;

&lt;p&gt;You can view your stashes with the command &lt;code&gt;git stash list&lt;/code&gt;. Stashes are saved in a last-in-first-out (LIFO) approach:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pZIquLMc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/faqrtsvk7t22rnt7io3t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pZIquLMc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/faqrtsvk7t22rnt7io3t.png" alt="Image description" width="650" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to apply the stash ?
&lt;/h2&gt;

&lt;p&gt;Run the following command to apply the recorded changes of your newest stash to the current working branch and delete that stash from the stash stack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`git stash pop`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could also wish to apply your most recent stash to the current working branch without deleting it from the stack. To accomplish this, use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`git stash apply`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can choose which stash you want to pop or apply by passing the identifier as the last argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`git stash pop stash@{1}` 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`git stash apply stash@{1} `
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to delete a stash ?
&lt;/h2&gt;

&lt;p&gt;It is good practice to remove stashes that are no longer needed. You must do this manually with the following commands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To empty the stash list by removing all the stashes
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`git stash clear`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To delete a particular stash from the stash list.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`git stash drop &amp;lt;stash_id&amp;gt;` 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to create a branch from stash ?
&lt;/h2&gt;

&lt;p&gt;It is possible to create a new branch from your latest stash. Just use this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash branch &amp;lt;branch_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to create a branch from an earlier stash, that's also possible by using stash reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash branch &amp;lt;branch_name&amp;gt; stash@{revision}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to add a description to stash ?
&lt;/h2&gt;

&lt;p&gt;By default, stashes are marked as WIP on top of the branch and commit that you created the stash from. However, this limited amount of information isn't helpful when you have multiple stashes, as it becomes difficult to remember or individually check their contents. To add a description to the stash, you can use the command &lt;code&gt;git stash save &amp;lt;description&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash save "remove semi-colon from schema"
Saved working directory and index state On master: remove semi-colon from schema
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you list all the available stashes you will see something similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash list
stash@{0}: On master: remove semi-colon from schema
stash@{1}: WIP on master: d7435644 Feat: configure graphql endpoint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to Check a stash diffs ?
&lt;/h2&gt;

&lt;p&gt;Sometimes you may need to view the stash diff, Use the command below to view the stash diff:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash show &amp;lt;stash_id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git stash show stash@{1}
console/console-init/ui/.graphqlrc.yml        |   4 +-
console/console-init/ui/generated-frontend.ts | 742 +++++++++---------
console/console-init/ui/package.json          |   2 +-
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also get a more detailed diff, pass the --patch or -p flag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; git stash show stash@{0} --patch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope you found this article useful and learned something new. If I missed any useful options for using stash, please let me know in the comments. For a fast reference, see &lt;a href="https://gist.github.com/Preethi-Dev/fa8ae46a75761356dc1fa711376c8345"&gt;git stash command cheat sheet&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>githunt</category>
      <category>github</category>
      <category>gitstash</category>
    </item>
  </channel>
</rss>
