DEV Community

Cover image for Automate Your Git Workflow with this Simple Bash Script
Abhinav Pandey
Abhinav Pandey

Posted on

Automate Your Git Workflow with this Simple Bash Script

The github repository for this script -> Script

Hey there fellow coders! 🚀

Ever found yourself doing the same repetitive git commands every time you want to push some changes? I know, it can be a bit annoying. But guess what? Bash scripts to the rescue! I know how you feel if you are remembering the repetative push and commits while reading these lines. Simply put, we have all been there. So, I decided that why not make a script of our own. From then on, I whipped up a neat little script that does the heavy lifting for you. Let's dive right in!

What Does This Script Do? 🤔

This script aims to automate three core git actions:

  1. Switching to the branch you specify.
  2. Checking if there are any changes to commit.
  3. Committing those changes with a message you provide and pushing to the specified branch.

Sounds fun, right?

Breaking Down The Code 🛠

#!/bin/bash
Enter fullscreen mode Exit fullscreen mode

The shebang! This tells the system that our script should run with the Bash shell.

Now, the script first checks if you've given it the required inputs: a branch name and a commit message.

if [ "$#" -ne 2 ]; then
  echo "Usage: $0 <branch_name> <commit_message>"
  exit 1
fi
Enter fullscreen mode Exit fullscreen mode

The magic $# gives us the count of arguments you've passed in. If it's not two, the script will remind you how to use it.

branch_name="$1"
commit_message="$2"
Enter fullscreen mode Exit fullscreen mode

Here we're just storing the arguments in nicely named variables to make things clearer later.

Now, let’s define our helper functions:

1. check_changes(): This checks if there are any changes to commit. If there aren't, it'll let you know and end the script.

check_changes() {
  if [ -z "$(git status --porcelain)" ]; then
    echo "No changes to commit."
    exit 0
  fi
}
Enter fullscreen mode Exit fullscreen mode

2. commit_and_push(): It does exactly what the name says. First, it stages all changes. Then, it commits with the message you provided. Lastly, it pushes to the branch.

commit_and_push() {
  git add .
  git commit -m "$commit_message"
  git push origin "$branch_name"
}
Enter fullscreen mode Exit fullscreen mode

Alrighty, the main show! 🎪

The script first checks if you're inside a Git repository.

if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  echo "Error: Current directory is not a Git repository."
  exit 1
fi
Enter fullscreen mode Exit fullscreen mode

If you are, it switches to the branch you mentioned:

git checkout "$branch_name"
Enter fullscreen mode Exit fullscreen mode

Then, our good friend check_changes checks if there are any... well, changes.

And finally, commit_and_push comes into play and, well, commits and pushes the changes.

How Do I Use This Script? 🚀

Clone the given Github repository and navigate into the script directory. To use it:

  1. Make it executable: chmod +x git-auto.sh
  2. Run the script: ./git_commit_and_push.sh <your-branch-name> "Your commit message over here"

Voila! Less hassle, more coding! 🎉

Wrapping Up

This script is a neat way to save some keystrokes and automate repetitive git actions. Feel free to expand upon it or tailor it to better fit your workflow. Happy coding, and may the force of automation be with you! 🤓

P.S. Always remember to have backups and test new scripts in a safe environment before using them on critical projects!

Top comments (12)

Collapse
 
g33knoob profile image
G33kNoob

Seem good but if you can add chatgpt to review and resume to get main message is good idea ✌️😁

Collapse
 
devrx profile image
Abhinav Pandey

OMG. That's a great idea. I will make sure to implement it and once I do, I'll share the track here with y'all. Thanks for the idea @g33knoob . I really appreciate it.

Collapse
 
larry_lo_5896c556cf188250 profile image
Larry Lo

tried. it works. I wonder if such sh can be added to the bash environment. That helps commanding as

git_commit_and_push "Your commit message over here"

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Oh wow. This is some great stuff.

Collapse
 
devrx profile image
Abhinav Pandey

Thanks for the support!! 😁

Collapse
 
ashwin2397 profile image
Ashwin2397

Great idea! But rather than making it in an executable script, I'd recommend putting it in your ~/.bashrc. This makes the functions readily available as commands on your shell.

Collapse
 
raphaelcarlosr profile image
raphaelcarlosr.dev

Do you know husky?

Collapse
 
karlkras profile image
Karl Krasnowsky

Aliases can provide quick shortcuts too, e.g.,
snyk.io/blog/10-git-aliases-for-fa...

Collapse
 
leolion3 profile image
Leonard

Heres a couple more for you:

Collapse
 
nicolasdanelon profile image
Nicolás Danelón

nice article, well explained and I like the proposal but I came to say I read the title as The Beatles song.. all you need is bash pa parararaa

Collapse
 
frherpich profile image
Herpich F. R.

Very good, nice and simple script that can be useful at times. I'd just change the order of the check if the branch has any change to commit. I there's no change, the commit message could be avoided.

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more