How to Quickly Push to Git with a Bash Script


Sometimes I don’t need to write commit messages for my Git projects (especially when updating repositories that simply function as a code store).

This is usually the case with blogs that are hosted through Git or maybe Markdown notes that I want to hold onto. Even then, writing commit messages could be useful, but they’re less necessary than with traditional projects.

My Workflow

I often use the script below to quickly add, commit, and push everything to the master branch of my repositories (of course, you can change this).

This automatically sets the commit message to be the current date and time, so you won’t have to write your own.

# An example commit message
rebuilding site Fri, Sep 11, 2020 8:03:21 PM

Suppose my script is in /deploy.sh.

You may have to run chmod +x deploy.sh before running the script if it isn’t executable.

After making any code changes, I’ll run the following:

./deploy.sh

This will prompt me to press Enter once more to confirm the push (just in case I ran the script prematurely).

Push to GitHub? (enter or ctrl+c):

All that being said, you can write your own commit messages by adding it as an argument to your script execution.

./deploy.sh "initial commit"

Here’s the script!

The Script

#!/bin/sh

# If a command fails then the deploy stops
set -e

# Confirm with user
read -n 1 -p "Push to GitHub? (enter or ctrl+c):" input

printf "\033[0;32mDeploying updates to GitHub...\033[0m\n"

# Add changes to git
git add .

# Commit changes
msg="rebuilding site $(date)"
if [ -n "$*" ]; then
	msg="$*"
fi
git commit -m "$msg"

# Push source
git push origin master