BASH

Automate Git Commit and Push for Web Projects

Streamline your development workflow with a Bash script to automate common Git operations like adding all changes, committing with a message, and pushing to a remote repository.

#!/bin/bash

# Check if a commit message is provided
if [ -z "$1" ]; then
  echo "Usage: $0 <commit_message>"
  exit 1
fi

COMMIT_MESSAGE="$1"

# Set the branch name (e.g., main, master, develop)
BRANCH_NAME="main"

echo "Adding all changes to Git..."
git add .

echo "Committing changes with message: \"$COMMIT_MESSAGE\"..."
git commit -m "$COMMIT_MESSAGE"

# Check if the commit was successful before pushing
if [ $? -eq 0 ]; then
  echo "Pushing changes to origin/$BRANCH_NAME..."
  git push origin "$BRANCH_NAME"
  if [ $? -eq 0 ]; then
    echo "Successfully committed and pushed changes to origin/$BRANCH_NAME."
  else
    echo "Error: Git push failed."
    exit 1
  fi
else
  echo "Error: Git commit failed."
  exit 1
fi
How it works: This Bash script simplifies the common Git workflow of staging all changes, committing them with a message, and pushing to a remote repository. It requires a commit message as the first command-line argument. The script first adds all modified and new files (`git add .`), then performs a commit (`git commit -m "$COMMIT_MESSAGE"`). If the commit is successful, it proceeds to push the changes to the specified branch (`git push origin "$BRANCH_NAME"`), providing feedback on each step and handling potential errors.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs