DEV Community

arjun
arjun

Posted on

Automate Your GitHub Workflow with Bash Scripting

Introduction:
In today's fast-paced development environment, efficiency is key. One way to streamline your workflow is by automating repetitive tasks, such as adding, committing, and pushing changes to GitHub repositories. In this article, we'll explore how to create a simple automation script using Bash scripting on Linux to handle these tasks effortlessly.

Prerequisites:
Before we dive into the implementation, ensure you have the following:

A GitHub account
Git installed on your Linux machine
Basic knowledge of Bash scripting

Script Overview:
Let's dissect the script step by step:

#!/bin/bash

# Check if a commit message and repository URL are provided
if [ -z "$1" ] || [ -z "$2" ]; then
    echo "Usage: $0 <commit_message> <repository_url>"
    exit 1
fi

Enter fullscreen mode Exit fullscreen mode

The script starts with a shebang line to specify that it's a Bash script. Then, it checks if both a commit message and a repository URL are provided as arguments. If not, it displays a usage message and exits.

# Add all changes
git add .

Enter fullscreen mode Exit fullscreen mode

Next, the script adds all changes in the current directory to the staging area using git add ..

# Commit with the provided message
git commit -m "$1"

Enter fullscreen mode Exit fullscreen mode

It then commits the changes with the commit message provided as the first argument.

# Set the remote repository URL
git remote add origin "$2"

Enter fullscreen mode Exit fullscreen mode

The script sets the remote repository URL to the one provided as the second argument.

# Push changes to the remote repository (assuming 'main' is your branch, modify as needed)
git push origin main --force

Enter fullscreen mode Exit fullscreen mode

Finally, it pushes the changes to the remote repository, assuming that the branch name is 'main'.
Usage:
To use this script, follow these steps:

Save the script into a file, e.g., git_auto.sh.
Make the script executable by running chmod +x git_auto.sh.
Execute the script, providing the commit message and repository URL as arguments:
./git_auto.sh "Your commit message" https://github.com/your_username/your_repository.git

Conclusion
With this automation script, you can significantly streamline your GitHub workflow. By eliminating the need to manually execute Git commands, you can focus more on your development tasks. Feel free to customize the script further to suit your specific requirements. Happy coding!

Top comments (10)

Collapse
 
siddharth_g profile image
Siddharth Gujrathi

Just wondering to run this script you need to provide the URL for a repo every time right?

We can optimise by either bypassing it or confirming if user wants to push to same remote origin set last time?

Collapse
 
arjun98k profile image
arjun

Once you set it always done the work

Collapse
 
siddharth_g profile image
Siddharth Gujrathi

Got it, make sense. Thanks.

Thread Thread
 
arjun98k profile image
arjun • Edited

Thanks you take your precious time taken and readed article have good day

Collapse
 
shadowruge profile image
izaias • Edited

repo git please ('|')

Collapse
 
juansanchez1001 profile image
Juan Pablo Sanchez Perez

Nice article. I was just looking for this.

Collapse
 
crobbi profile image
Chandrashekhar

How it will decide commit Message it will be same everytime or different?

Collapse
 
arjun98k profile image
arjun

You can write ahe message in first code block where it written commit_message

Collapse
 
crobbi profile image
Chandrashekhar

That means still user have to again come and change commit message or else same commit message is printed right?

Thread Thread
 
arjun98k profile image
arjun

Yes the end goal of script is to ged rid away from typing 3 command git add, commit, push