Upload Files to GitHub Using a Bash Script

Introduction
If you find yourself repeatedly typing the same Git commands every time you finish working on a project, a simple Bash script can automate the process.
In this tutorial, you’ll create a deployment script that stages your changes, creates a commit, and pushes everything to GitHub with a single command.
Prerequisites
Before starting, make sure you have:
- Git installed
- A GitHub account
- A Git repository already initialized
- Basic terminal knowledge
- Visual Studio Code (or any text editor)
Step 1: Navigate to Your Project
Open a terminal and move into your project directory:
cd ~/Documents/projects/my-project
Here is your blog post cleanly formatted with valid Hugo YAML front matter and structured Markdown syntax for seamless compilation:
```markdown
---
title: "Upload Files to GitHub Using a Bash Script"
description: "Automate committing and pushing your project to GitHub using a simple Bash deployment script."
date: 2022-08-18
summary: "Learn how to automate Git commits and pushes with a reusable Bash deployment script."
cover: "/images/post/bash.jpg"
thumbnail: "/images/post/bash.jpg"
categories:
- "Git"
tags:
- "Git"
- "GitHub"
- "Bash"
- "Linux"
- "Terminal"
authors:
- "Tejiri Mayone"
featured: false
draft: false
---
## Introduction
If you find yourself repeatedly typing the same Git commands every time you finish working on a project, a simple Bash script can automate the process.
In this tutorial, you'll create a deployment script that stages your changes, creates a commit, and pushes everything to GitHub with a single command.
---
## Prerequisites
Before starting, make sure you have:
* Git installed
* A GitHub account
* A Git repository already initialized
* Basic terminal knowledge
* Visual Studio Code (or any text editor)
---
## Step 1: Navigate to Your Project
Open a terminal and move into your project directory:
```bash
cd ~/Documents/projects/my-project
Step 2: Create the Deployment Script
Create a new script file:
touch deploy.sh
Open it in your preferred text editor:
code deploy.sh
Step 3: Add the Script
Paste the following logic into your deploy.sh file:
#!/bin/sh
# Stop execution if a command fails
set -e
printf "\033[0;32mDeploying project to GitHub...\033[0m\n"
# Stage all changes
git add .
# Create a default commit message
msg="Update by $(whoami) on $(date)"
# If a custom message is provided, use it instead
if [ -n "$*" ]; then
msg="$*"
fi
git commit -m "$msg"
# Push to GitHub
git push origin main
printf "\033[0;32mDeployment complete.\033[0m\n"
Step 4: Make the Script Executable
Grant the script execute permissions so it can run autonomously:
chmod +x deploy.sh
Step 5: Deploy Your Project
Run the script directly to use the automated timestamp message:
./deploy.sh
Alternatively, pass a custom message as an argument:
./deploy.sh "Fix authentication bug"
Example Output
Deploying project to GitHub...
[main 4f83e3d] Fix authentication bug
5 files changed...
Deployment complete.
Authentication Note
GitHub no longer supports account passwords when pushing over HTTPS. Instead, ensure you use one of the following secure authentication methods:
- Personal Access Token (PAT): Recommended if you are connecting over HTTPS.
- SSH Keys: Recommended for seamless, credential-free terminal use.
Once configured, your script will execute and push without prompting for interactive credentials.
Bonus: Include a Hugo Build Workflow
If you’re deploying a Hugo website, you should compile your static assets right before staging your changes.
To build your standard production files:
hugo
For custom-themed projects:
hugo -t your-theme
Complete Integrated Script
Integrating the static compilation step yields the following production script:
#!/bin/sh
set -e
printf "\033[0;32mDeploying project to GitHub...\033[0m\n"
# Build the Hugo static site
hugo
# Stage files
git add .
msg="Update by $(whoami) on $(date)"
if [ -n "$*" ]; then
msg="$*"
fi
git commit -m "$msg"
git push origin main
printf "\033[0;32mDeployment complete.\033[0m\n"
Conclusion
Automating your Git workflow with a Bash script saves time and eliminates repetitive typing. Instead of manually staging files, writing commits, and pushing flags, you can wrap the entire process up in a single custom terminal call.
As your infrastructure scales, you can easily extend this file to run automated tests, lint code, or clear caching pipelines prior to running your distribution pushes.