DEV Community

Rahul Sharma
Rahul Sharma

Posted on • Updated on

Single shell script command to stage all files, commit and push to current branch

Hey all!

This is my first post in here; so please bear with me. :)

Background

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.

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.

First good step was that I moved all my personal projects to Github. 💡

Problem

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.

  1. Stage all files:

    git commit .
    
  2. Add commit

    git commit -m {commit message}
    
  3. Push changes to current branch, in this case master

    git push origin master
    

I am sometimes lazy to type and execute the 3 commands. What I want is ability to do this in a single command. 😅

Scripting for the rescue

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

I created a bash file that can help me with this minor inconvenience:

With this script I can now run command from command line as: sh expressPush.sh "My commit message", It does the following:

  1. Stages all files
  2. Adds a commit with provided message (My commit message) as an argument.
  3. Reads current branch name and executes a git push.

Additionally command is executed without a message as: sh expressPush.sh, this uses current date time stamp for message. I use this when message is not relevant.

Now its not nice to copy this file in every project. 😩

Make the script available to projects

As most my project have a package.json file, I can store this shell script in a local folder and add a script for this in each project, package.json looks would like below:

"scripts": {
    "expresspush": "sh 'E:\\GITUtils\\expressPush.sh'"
}
  • Now we can simply run npm run expresspush "My commit message" or npm run expresspush within the project directory.

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

Make the script available globally 🤩

Learnt later that we can alias the shell script file to make the command available globally.

To do so, create or update .bashrc file in directory: C:\Users\username and add:

alias expresspush='sh "E:\GITUtils\expressPush.sh"'

Now we can simply run the script with alias from command line in any GIT project:

  • expresspush or expresspush "My commit message"

Closing:

This solved my problem. If you have any thoughts or suggestions to improve, please do let me know. 👋🏼

Top comments (4)

Collapse
 
vonheikemen profile image
Heiker

Have you tried making a "custom command" for git?

You could try make one like it says on this article: Extending Git.

Collapse
 
forcetrekker profile image
Rahul Sharma

This looks quite interesting and thanks for sharing with me, I will give this try! :)

Collapse
 
forcetrekker profile image
Rahul Sharma

Custom command felt hard and unfortunately I could not make it work. So I created an alias in .bashrc and it worked like charm. :)

Collapse
 
maheshhirugade profile image
Mahesh Hirugade

That's a nice trick to save push efforts every time. Thanks @rahul