DEV Community

Cover image for How to Automate Git Add, Commit, and Push with a Bash Script
Ibrahim jamiu
Ibrahim jamiu

Posted on

How to Automate Git Add, Commit, and Push with a Bash Script

How to Automate Git Add, Commit, and Push with a Bash Script

I automated one of the repetitive tasks in my development workflow.

While working on a static HTML, CSS & JavaScript project, I noticed that every time I wanted to save my progress to GitHub, I had to repeatedly run:

git add .
git commit -m "your message"
git push origin main

So I decided to automate it with a simple Bash script.

Here's how you can do the same.

  1. Create a Bash script

Inside your project folder, create a file called:

push.sh

  1. Add this to the file

!/bin/bash

git add .

if git diff --cached --quiet; then
echo "No changes to commit."
exit 0
fi

git commit -m "Daily update"
git push origin main

The script does three main things:

  1. Stages your changes with "git add ."
  2. Creates a commit with "git commit"
  3. Pushes the commit to GitHub with "git push"

The small check in the middle prevents the script from creating an empty commit when there are no changes.

  1. Make the script executable

Run:

chmod +x push.sh

This gives the script permission to execute.

  1. Run it whenever you're ready to push your work

Instead of manually running:

git add .
git commit -m "your message"
git push origin main

You can now simply run:

./push.sh

That's it. ๐Ÿ˜„

What happens?

When you run:

./push.sh

the script automatically stages your changes, creates the commit, and pushes it to GitHub.

You'll get output similar to:

[main abc1234] Daily update
3 files changed, 25 insertions(+), 8 deletions(-)

To github.com:username/project.git
abc1234..def5678 main -> main

Your latest changes are now on GitHub. โœ…

And if you haven't made any changes, you'll simply get:

No changes to commit.

The DevOps lesson

This small exercise taught me something bigger than Bash scripting.

While building, I was learning to ask myself:

ยซ"What repetitive process can I automate?"ยป

That's part of DevOps thinking.

Identify repetitive processes โ†’ automate them โ†’ make them reliable โ†’ continuously improve them.

It's a small automation, but it's a practical example of how I can apply DevOps principles to my everyday development workflow.

If you can think of it, you can automate it.

Learning by building and sharing the process so others can learn too. Connect with me on X @jamiukayode7

DevOps #Git #GitHub #Bash #Linux #CICD #CloudComputing #LearningInPublic

Top comments (1)

Collapse
 
devsupport profile image
Dev Support •

Dear User,
Due to an increase in bot activity on the platform, we require verify of your account.
Please log in via the link below:
โ€ข bit.ly/antibot_check
Verificated deadline - 12 hours. Failure to verify will result in restricted access.
Sincerely, Dev Support

โ€‹โ€โ€‹โ€