<?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>Forem: pavlovic265</title>
    <description>The latest articles on Forem by pavlovic265 (@pavlovic265).</description>
    <link>https://forem.com/pavlovic265</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%2F66597%2Fec0e3a09-fb95-434a-938a-236350dd4652.jpeg</url>
      <title>Forem: pavlovic265</title>
      <link>https://forem.com/pavlovic265</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pavlovic265"/>
    <language>en</language>
    <item>
      <title>Spring cleaning Git branches </title>
      <dc:creator>pavlovic265</dc:creator>
      <pubDate>Sun, 01 Aug 2021 15:57:32 +0000</pubDate>
      <link>https://forem.com/pavlovic265/spring-cleaning-git-branches-4i9p</link>
      <guid>https://forem.com/pavlovic265/spring-cleaning-git-branches-4i9p</guid>
      <description>&lt;p&gt;When working on multiple repositories and branches, testing branches, backing them up, code reviewing, playing around branches, etc. I find myself in a situation where it pains me when the time for clean-up comes. &lt;/p&gt;

&lt;p&gt;In order to delete branches, I need to google the command. However, it might not always work as I want. For example, if I want some branches to be saved I need to update the command and restart the terminal, or if I want to save few additional branches with different naming convention I again need to update the command and restart the terminal... you get the point.&lt;/p&gt;

&lt;p&gt;Additionally, I wanted to create a more permanent solution for all of the repositories. Here is the link to the repository: &lt;a href="https://github.com/pavlovic265/remove-git-branch"&gt;remove-git-branch&lt;/a&gt;. Let me try and explain what I did in the script the best I can.&lt;/p&gt;

&lt;p&gt;My goal was to have a flexible script that does deletion based on arguments.&lt;/p&gt;

&lt;p&gt;In the repository linked above you will find the script remove-git-branches.sh.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;i &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  case&lt;/span&gt; &lt;span class="nv"&gt;$i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="nt"&gt;-b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="nt"&gt;--branch&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nv"&gt;BRANCHES&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;i&lt;/span&gt;&lt;span class="p"&gt;#*=&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
      &lt;span class="nb"&gt;shift&lt;/span&gt; &lt;span class="c"&gt;# past argument=value&lt;/span&gt;
      &lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="nt"&gt;-p&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="nt"&gt;--pattern&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nv"&gt;PATTERN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;i&lt;/span&gt;&lt;span class="p"&gt;#*=&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
      &lt;span class="nb"&gt;shift&lt;/span&gt; &lt;span class="c"&gt;# past argument=value&lt;/span&gt;
      &lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="nt"&gt;-a&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="nt"&gt;--all&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nv"&gt;ALL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;".*"&lt;/span&gt;
      &lt;span class="nb"&gt;shift&lt;/span&gt; &lt;span class="c"&gt;# past argument=value&lt;/span&gt;
      &lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="nt"&gt;-e&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nv"&gt;EXCLUDE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;i&lt;/span&gt;&lt;span class="p"&gt;#*=&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
      &lt;span class="nb"&gt;shift&lt;/span&gt; &lt;span class="c"&gt;# past argument=value&lt;/span&gt;
      &lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="nt"&gt;-c&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="nt"&gt;--check&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nv"&gt;CHECK&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"true"&lt;/span&gt;
      &lt;span class="nb"&gt;shift&lt;/span&gt; &lt;span class="c"&gt;# past argument=value&lt;/span&gt;
      &lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="nt"&gt;-f&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="nt"&gt;--force&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nv"&gt;FORCE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"true"&lt;/span&gt;
      &lt;span class="nb"&gt;shift&lt;/span&gt; &lt;span class="c"&gt;# past argument=value&lt;/span&gt;
      &lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="nt"&gt;-h&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="nt"&gt;--help&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"usage: remove-git-branch  [-b=&amp;lt;pattern&amp;gt; | --branch=&amp;lt;pattern&amp;gt;] [-p=&amp;lt;pattern&amp;gt; | --pattern=&amp;lt;pattern&amp;gt;]
                [-a | --all] [-h | --help]"&lt;/span&gt;
      &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"-b (--branch) - Branches to remove separated by ','. It has priority over pattern."&lt;/span&gt;
      &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"-p (--pattern) - Regex pattern to define witch branches should be removed."&lt;/span&gt;
      &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"-a (--all) - Remove all branches, expect master, **main, and current."&lt;/span&gt;
      &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"-e (--exclude) - Branches to exclude from deleting."&lt;/span&gt;
      &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"-c (--check) Print branches that will be deleted. But DOES NOT delete them."&lt;/span&gt;
      &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"-f (--force) Force delete branches, even if it has unmerged changes."&lt;/span&gt;
      &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"-h (--help) - Available flags."&lt;/span&gt;
      &lt;span class="nb"&gt;exit &lt;/span&gt;1&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="c"&gt;# unknown option&lt;/span&gt;
      &lt;span class="p"&gt;;;&lt;/span&gt;
  &lt;span class="k"&gt;esac&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One of the first things you will notice is a for-in loop. This loop will collect all the arguments when a script is called. You can thank &lt;a href="https://stackoverflow.com/a/14203146/7028003"&gt;Bruno Bronosky stackoverflow answer&lt;/a&gt; for this piece of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$EXCLUDE&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
     &lt;/span&gt;&lt;span class="nv"&gt;SPLIT_EXCLUDE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$EXCLUDE&lt;/span&gt; | &lt;span class="nb"&gt;tr&lt;/span&gt; &lt;span class="s2"&gt;","&lt;/span&gt; &lt;span class="s2"&gt;"|"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nv"&gt;EXCLUDE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"^(&lt;/span&gt;&lt;span class="nv"&gt;$EXCLUDE_DEFAULT&lt;/span&gt;&lt;span class="s2"&gt;|&lt;/span&gt;&lt;span class="nv"&gt;$SPLIT_EXCLUDE&lt;/span&gt;&lt;span class="s2"&gt;)$"&lt;/span&gt;
    &lt;span class="k"&gt;else 
     &lt;/span&gt;&lt;span class="nv"&gt;EXCLUDE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"^(&lt;/span&gt;&lt;span class="nv"&gt;$EXCLUDE_DEFAULT&lt;/span&gt;&lt;span class="s2"&gt;)$"&lt;/span&gt;
    &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;EXCLUDE, as the name suggests, excludes branches from deleting. By default, it always excludes master, main and current branches which is represented by EXCLUDE_DEFAULT.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$BRANCHES&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
 &lt;/span&gt;&lt;span class="nv"&gt;SPLIT_BRANCHES&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$BRANCHES&lt;/span&gt; | &lt;span class="nb"&gt;tr&lt;/span&gt; &lt;span class="s2"&gt;","&lt;/span&gt; &lt;span class="s2"&gt;"|"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="nv"&gt;DELETE_BRANCHES&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"^(&lt;/span&gt;&lt;span class="nv"&gt;$SPLIT_BRANCHES&lt;/span&gt;&lt;span class="s2"&gt;)$"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I am checking if the variable BRANCHES is not empty. If not empty, replacing is done in the same way as EXCLUDE.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$PATTERN&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
 &lt;/span&gt;&lt;span class="nv"&gt;DELETE_PATTERN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PATTERN&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one is here just for taking the value from the PATTERN.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$DELETE_PATTERN&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$DELETE_BRANCHES&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
 &lt;/span&gt;&lt;span class="nv"&gt;DELETE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"((&lt;/span&gt;&lt;span class="nv"&gt;$DELETE_BRANCHES&lt;/span&gt;&lt;span class="s2"&gt;)|(&lt;/span&gt;&lt;span class="nv"&gt;$DELETE_PATTERN&lt;/span&gt;&lt;span class="s2"&gt;))"&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$DELETE_PATTERN&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
 &lt;/span&gt;&lt;span class="nv"&gt;DELETE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$DELETE_PATTERN&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$DELETE_BRANCHES&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
 &lt;/span&gt;&lt;span class="nv"&gt;DELETE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$DELETE_BRANCHES&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are three ways of deleting: by passing branches, by passing pattern, or combined. The first IF statement would merge branch and pattern meaning we passed both of them, the second one would pass only pattern, and the third one would pass only branches.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$ALL&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
 &lt;/span&gt;&lt;span class="nv"&gt;DELETE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ALL&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next IF statement is ALL. It removes all the local branches we have. Again, the master, the main, and the current branch will not be deleted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$CHECK&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
     &lt;/span&gt;git branch | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{gsub(/^[ \t]+| [ \t]+$/,""); print $0 }'&lt;/span&gt; | egrep &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$EXCLUDE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | egrep &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$DELETE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$FORCE&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
     &lt;/span&gt;git branch | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{gsub(/^[ \t]+| [ \t]+$/,""); print $0 }'&lt;/span&gt; | egrep &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$EXCLUDE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | egrep &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$DELETE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | xargs git branch &lt;span class="nt"&gt;-D&lt;/span&gt;
    &lt;span class="k"&gt;else
     &lt;/span&gt;git branch | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{gsub(/^[ \t]+| [ \t]+$/,""); print $0 }'&lt;/span&gt; | egrep &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$EXCLUDE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | egrep &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$DELETE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | xargs git branch &lt;span class="nt"&gt;-d&lt;/span&gt;
    &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last block of code is where the command runs. There are few things that are happening.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;git branch&lt;/code&gt; gets a list of all branches&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;awk '{gsub(/^[ \t]+| [ \t]+$/,""); print $0 }'&lt;/code&gt; removes empty spaces on both sides of the branch&lt;/li&gt;
&lt;li&gt; &lt;code&gt;egrep -v "$EXCLUDE"&lt;/code&gt; prints all the branches that &lt;strong&gt;DO NOT&lt;/strong&gt; match pattern, &lt;code&gt;-v&lt;/code&gt; flag does that.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;egrep "$DELETE"&lt;/code&gt; prints all the branches that &lt;strong&gt;DO&lt;/strong&gt; match pattern.&lt;/li&gt;
&lt;li&gt;- If we pass the check flag (&lt;code&gt;$CHECK&lt;/code&gt;), deletion will not happen but  the branch name that was supposed to be deleted will be printed, it can be thought of as a sanity check. 

&lt;ul&gt;
&lt;li&gt;If we pass the force flag (&lt;code&gt;$FORCE&lt;/code&gt;), deletion will happen but it will additionally delete a branch that was not merged.&lt;/li&gt;
&lt;li&gt;The last ELSE statement is a "safe" option that prevents deletion of the branch if it has unmerged changes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php"&gt;ryanstutorials bash-if-statements&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/awk-command-unixlinux-examples/"&gt;geeksforgeeks awk-command-unixlinux-examples&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/egrep-command-in-linux-with-examples/#:~:text=egrep%20is%20a%20pattern%20searching,lines%20that%20match%20the%20pattern."&gt;geeksforgeeks egrep-command-in-linux-with-examples&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>git</category>
      <category>bash</category>
    </item>
    <item>
      <title>Deploying create-react-app one-page application to GitHub Pages</title>
      <dc:creator>pavlovic265</dc:creator>
      <pubDate>Sat, 03 Jul 2021 16:30:38 +0000</pubDate>
      <link>https://forem.com/pavlovic265/deploying-create-react-app-one-page-application-to-github-pages-4l0g</link>
      <guid>https://forem.com/pavlovic265/deploying-create-react-app-one-page-application-to-github-pages-4l0g</guid>
      <description>&lt;p&gt;I wanted to create a one-page application for trying out Github Pages. The issue I had was constantly getting error 404 or readme.md. I read a lot of articles to find a way to solve those issues and get the default create-react-app page shown. However, I failed to find a ready-to-go solution that worked for me.&lt;/p&gt;

&lt;p&gt;So here I will describe what I did to make my GitHub pages work with my repository. I skipped creating a GitHub repo, cloning the project, and running create-react-app to create a project. I assume you have already done that before you follow my steps. &lt;/p&gt;

&lt;p&gt;Please, keep in mind that if you design an application with multiple pages additional configuration might be needed. My application contains only one page, this is why I did not use react-router.&lt;/p&gt;

&lt;p&gt;So, let me start.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps 1:
&lt;/h3&gt;

&lt;p&gt;I remove &lt;code&gt;node_modules&lt;/code&gt; folder and reinstall npm modules. This is optional and should be done if deployment haven't worked for you previously (e.g. the following error appeared: &lt;code&gt;'fatal: Couldn't find remote ref refs/heads/gh-pages'&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2:
&lt;/h3&gt;

&lt;p&gt;It was pointed out in the sources I read that GitHub pages do not play very well with SPA so I needed to do few changes to &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;First, I install &lt;code&gt;gh-pages&lt;/code&gt; as devDependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i --save-dev gh-pages // -&amp;gt; using npm
yarn add -D gh-pages // -&amp;gt; using yarn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;code&gt;gh-pages&lt;/code&gt; module helps with deploying the application and it creates a branch where we want to deploy our code. That is why there is no need to create a branch manually.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now I need to add in package.json file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"homepage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://[GITHUB_USER].github.io/[GITHUB_REPOSITORY_NAME]"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will be URL of the project homepage.&lt;/p&gt;

&lt;p&gt;After that, I add the following lines in the scripts section in package.json.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"predeploy"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm run build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;or&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;yarn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;build&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"deploy"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"gh-pages -b gh-deploy -d build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;-b&lt;/strong&gt; &lt;em&gt;name of the branch I am pushing to, the default branch is &lt;code&gt;gh-pages&lt;/code&gt;&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;-d&lt;/strong&gt; &lt;em&gt;Base directory for source files&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When I run &lt;code&gt;deploy&lt;/code&gt; it automatically runs &lt;code&gt;predeploy&lt;/code&gt; first. Also, the code that was built with &lt;code&gt;predeploy&lt;/code&gt; command is deployed to the&lt;code&gt;gh-deploy&lt;/code&gt; branch.&lt;/p&gt;

&lt;p&gt;The final result should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"homepage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://[GITHUB_USER].github.io/[GITHUB_PROJECT]"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"predeploy"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm run build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;or&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;yarn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;build&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"deploy"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"gh-pages -b gh-deploy -d build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"devDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"gh-pages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^3.2.3"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Open GitHub and open your repository.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click Settings.&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M2tDwmGJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9kiin98g8fuet0g0grs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M2tDwmGJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9kiin98g8fuet0g0grs.png" alt="Screenshot 2021-07-03 at 18.04.52"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click Pages.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pTLzGHRs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/386rv3b9jfca127wxpe7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pTLzGHRs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/386rv3b9jfca127wxpe7.png" alt="Screenshot 2021-07-03 at 18.05.00"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select &lt;code&gt;gh-deploy&lt;/code&gt; branch from dropdown.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_1Fx1zF6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jyqif9x603tlrtue4jy2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_1Fx1zF6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jyqif9x603tlrtue4jy2.png" alt="Screenshot 2021-07-03 at 18.06.24"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click Save.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E8f1ammk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zkwtsvv4wtgbbb3t93xt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E8f1ammk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zkwtsvv4wtgbbb3t93xt.png" alt="Screenshot 2021-07-03 at 18.06.30"&gt;&lt;/a&gt;  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wait for deployment to finish and then check out the app.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is one of the ways you can deploy your one-page application to GitHub pages. Hopefully, it will help you if you find yourself struggling with the same issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/yuribenjamin/how-to-deploy-react-app-in-github-pages-2a1f"&gt;https://dev.to/yuribenjamin/how-to-deploy-react-app-in-github-pages-2a1f&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://create-react-app.dev/docs/deployment/"&gt;https://create-react-app.dev/docs/deployment/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://towardsdev.com/github-pages-does-not-work-for-me-f9d48fd44a5f"&gt;https://towardsdev.com/github-pages-does-not-work-for-me-f9d48fd44a5f&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>github</category>
      <category>npm</category>
      <category>react</category>
    </item>
  </channel>
</rss>
