A Script to Automate Git Add, Commit & Push
19 Feb 2023
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.
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):
#!/bin/bash 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:
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.