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.
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
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:
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!
#!/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
More CLI Articles
- How to Install Powerline in WSL2 Terminal
- How to Zip and Unzip Files in WSL2
- How to List All Git Aliases
- How to Add, Commit, and Push in One Git Command
- How to Replace a Local Branch with a Remote Branch in Git
- How to Revert a Specific File or Folder in Git
- How to Install Hugo on WSL2
- How to Install Node.js and npm on WSL2
- How to Revert to a Previous Commit in Git without Modifying History
- How to Merge a Branch Into Another Branch in Git
- How to Fix "xcrun error invalid active developer path" with Git on macOS
- How to Update Node to Another Version on Windows WSL
- How to Use SSH with GitHub (Instead of HTTPS) on Windows WSL
- How to Delete Files with a Specific Extension in all Subdirectories Recursively
- How to Suppress Output of npm install
- How to Pass Environment Variables to Makefiles in Subdirectories
- How to Access Environment Variables in a Makefile
- How to Reset Password on Ubuntu Linux
- How to Pull a Branch from Another User's Fork using Git
- How to Align GitHub README images in Markdown
- How to Merge Development and Production Branches in Git
- How to Change Author of Commit in Git History
- WSL2: How to Fix "Virtual hard disk files must be uncompressed and unencrypted and must not be sparse"
- How to Replace a Branch with Another Branch in Git
- How to Delete a Git Branch Locally and Remotely
- How to Schedule Recurring Deploys in Netlify
- How to View All Global Packages in npm
- How to Test an npm package locally
- How to Update an npm Package (Fixing "You cannot publish over the previously published versions")
- How to Undo Changes in a Single File in Git
- How to Fix the Netlify error: "Treating warnings as errors because process.env.CI = true"
- How to Set the Default Directory for Git Bash on Windows
- How to Search Past Terminal Commands in Linux
- How to Get the Count of Files with each File Extension in Linux
- How to Find All Files with a Specific File Extension in Linux
- How to Run Multiple Commands Simultaneously in Linux
- How to Watch a File in Linux using tail
- Linux Timestamps: The Difference Between atime, mtime, ctime, and crtime
- How To Remove Duplicate Lines While Maintaining Order in Linux
- How To Count the Number of Non-Empty Output Lines in Linux
- How To Find All IP Addresses within a File in Linux with grep
- How To Rename Every File in all Subdirectories in Windows CMD