Script to Automate Git Add, Commit & Push

by
, posted

If you are familiar with git workflow, you know how tedious it becomes to type these three commands every single time we make changes to the local codebase & push to the central repository.

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

For instance, every time I update this site, I have to type these commands on & on irrespective of the commit weight. Even so, it doesn’t feel dull if a user push commits ocassionaly. However, for anyone who pushes to remote frequently it definitely feels weary & slow downs the flow.

Bash Script

I thought about automating the process using a bash script, as I’m running on Mac M1. Taking inspiration from stackoverflow here is how I got it done with some tweaks, making it more concise & convenient. (You can replace or remove the 🌱 emoji if it is not supported on your machine)

gpush() {
    git add . && \
    git commit -m "🌱 $*" && \
    git push
}

How to use

Launch your terminal and open shell config file by running vim ~/.bashrc (if you are running default shell) or vim ~/.zshrc (I’m personally using zsh). Paste the script in config file, save & exit by hitting :wq. Restart your terminal or run source ~/.bashrc or source~./zshrc.

Blimey, that be the end of it! You are ready to roll. Next time you are done with changes in your local repo, run this single command to push changes to your remote repository.

gpush "commit message"

Here is the command in action.

That is pretty much about it. If you found it useful consider sharing with friends & toast this post. Thanks for taking time to read this 😊

Reply via mail

Your Signature