A Script to Automate Git Add, Commit & Push

19 Feb 2023 | ~2 minute read

I wanted a way to automate the git add, commit & push commands. Here's how I did it...

Since learning how to use Git, it has become an integral part of my workflow for things like publishing to this site, and updating my various projects, like Simple.css and That Watch Bloke. However, pushing changes to a Git repository can get tedious, as it involves 3 commands. Every. Single. Time:

git add *
git commit -m "Your commit message..."
git push

I use VSCodium to write code, which integrates with Git, making things easier. But I still wanted a way to automate the process of committing to my Git repositories without having to enter those 3 commands, over and over again.

Plus, if I'm writing a post (like this very one), I'm not in VSCodium. I'm typing the post in Typora; so being able to flip to the command line and enter a single command, would be great.

BASH script

Since I use an M1 Macbook Air, which is basically Unix under the hood, I can use good old BASH to do this. So I used the following script (thanks to StackOverflow):

read -p "Commit message: " desc
git add . && \
git commit -m "$desc" && \
git push

I then added the following line to my .zshrc file so I can call this script with a single command:

alias push=~/path/to/git-push.sh

If running the script fails with permission issues, run chmod +x git-push.sh to give the script execution rights.

So now, whenever I'm in a Git repo within my command line, all I need to do is type the command push. My script will then be called, it will ask for a commit message, then push to the repo. All automatically.

Perfect. 👌

← The one before
Micro.Blog Is Still Confusing

Up next →
My Static Site Workflow

Get in touch!

Receiving emails from my readers is my favourite thing, so if you have something to say, feel free to drop me an email or sign my guestbook.

Want more content?

Say no more, dear reader. Here's three random posts from this blog for you to peruse:

Metro 2034
25 Sep 2023

Why I Don’t Use A Static Site Generator
08 Apr 2020

The Way Of Kings
17 Apr 2023

Want to be informed when I post new articles? Simply enter your email address below and you will get an email whenever new posts are published.

Alternatively, you can subscribe via RSS instead.

Enjoyed this post?

I put a lot of work into maintaining this site and I really enjoy interacting with my readers.

My fuel of choice is coffee, so if you did enjoy this post, or found it in any way useful, I'd appreciate more fuel to keep me going. ❤️

Buy me a coffee