<?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: Rahul Sharma</title>
    <description>The latest articles on DEV Community by Rahul Sharma (@forcetrekker).</description>
    <link>https://dev.to/forcetrekker</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%2F375002%2Fdf20c6dc-9c66-40ec-adc1-e97a3483d6b1.jpeg</url>
      <title>DEV Community: Rahul Sharma</title>
      <link>https://dev.to/forcetrekker</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/forcetrekker"/>
    <language>en</language>
    <item>
      <title>Single shell script command to stage all files, commit and push to current branch</title>
      <dc:creator>Rahul Sharma</dc:creator>
      <pubDate>Wed, 19 Aug 2020 00:06:21 +0000</pubDate>
      <link>https://dev.to/forcetrekker/single-shell-script-command-to-stage-all-files-commit-and-push-to-current-branch-3d0h</link>
      <guid>https://dev.to/forcetrekker/single-shell-script-command-to-stage-all-files-commit-and-push-to-current-branch-3d0h</guid>
      <description>&lt;p&gt;Hey all!&lt;/p&gt;

&lt;p&gt;This is my first post in here; so please bear with me. :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;I used to use sourcetree for working with GIT; lately it is consuming a lot of RAM and CPU resources, GIT extensions is another client that is nice and light weight. But working directly with command line feels liberating, I am mostly able to understand and learn on what is exactly going on with command line instead of a GUI.&lt;/p&gt;

&lt;p&gt;Earlier I used to use just my laptop for work and desktop for personal work. Due to the pandemic we all mostly work from home. In my case I keep switching between PC and desktop.&lt;/p&gt;

&lt;p&gt;First good step was that I moved all my personal projects to Github. 💡&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Due to use of multiple computers and GIT, usage of the 3 powerful commands increased (git add, commit and push). And sometimes I just want to push changes with default commit message to save my work.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Stage all files:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit .
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add commit&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit -m {commit message}
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Push changes to current branch, in this case master&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push origin master
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I am sometimes lazy to type and execute the 3 commands. What I want is ability to do this in a single command. 😅&lt;/p&gt;

&lt;h2&gt;
  
  
  Scripting for the rescue
&lt;/h2&gt;

&lt;p&gt;This was an opportunity to learn some automation and execute this with a single command, found that I could use some shell scripting. 😇&lt;/p&gt;

&lt;p&gt;I created a bash file that can help me with this minor inconvenience:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With this script I can now run command from command line as: &lt;code&gt;sh expressPush.sh "My commit message"&lt;/code&gt;, It does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Stages all files&lt;/li&gt;
&lt;li&gt;Adds a commit with provided message (My commit message) as an argument.&lt;/li&gt;
&lt;li&gt;Reads current branch name and executes a git push.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Additionally command is executed without a message as: &lt;code&gt;sh expressPush.sh&lt;/code&gt;, this uses current date time stamp for message. I use this when message is not relevant.&lt;/p&gt;

&lt;p&gt;Now its not nice to copy this file in every project. 😩&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the script available to projects
&lt;/h2&gt;

&lt;p&gt;As most my project have a &lt;code&gt;package.json&lt;/code&gt; file, I can store this shell script in a local folder and add a script for this in each project, &lt;code&gt;package.json&lt;/code&gt; looks would like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;expresspush&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sh 'E:&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;GITUtils&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;expressPush.sh'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now we can simply run &lt;code&gt;npm run expresspush "My commit message"&lt;/code&gt; or &lt;code&gt;npm run expresspush&lt;/code&gt; within the project directory. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is good enough but has a problem with dependency as this assumes or hardcodes the directory of shell script file in &lt;code&gt;package.json&lt;/code&gt;. And not all projects may based on &lt;code&gt;npm&lt;/code&gt;. 😓&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the script available globally 🤩
&lt;/h2&gt;

&lt;p&gt;Learnt later that we can alias the shell script file to make the command available globally.&lt;/p&gt;

&lt;p&gt;To do so, create or update &lt;code&gt;.bashrc&lt;/code&gt; file in directory: &lt;code&gt;C:\Users\username&lt;/code&gt; and add:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;alias expresspush='sh "E:\GITUtils\expressPush.sh"'&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now we can simply run the script with alias from command line in any GIT project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;expresspush&lt;/code&gt; or &lt;code&gt;expresspush "My commit message"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Closing:
&lt;/h2&gt;

&lt;p&gt;This solved my problem. If you have any thoughts or suggestions to improve, please do let me know. 👋🏼&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
