BASH

Automate Git Commit and Push

Simplify common Git workflows by automatically adding all changes, committing with a predefined message or a provided one, and pushing to the current branch.

#!/bin/bash

COMMIT_MSG=${1:-"Update files"}

if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
  echo "Error: Not a Git repository."
  exit 1
fi

git add .
git commit -m "$COMMIT_MSG"

CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)

if git push origin "$CURRENT_BRANCH"; then
  echo "Changes committed with message: \"$COMMIT_MSG\" and pushed to $CURRENT_BRANCH."
else
  echo "Error: Failed to push changes. Check your Git configuration."
  exit 1
fi
How it works: This script streamlines the Git commit and push process. It first checks if the current directory is a Git repository. Then, it stages all modified files, commits them with a default message (or one provided as an argument), and finally pushes the changes to the `origin` remote on the current branch.

Need help integrating this into your project?

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

Hire DigitalCodeLabs