BASH
Automate Git Repository Update and Push
Streamline your development workflow with a script to pull latest changes, add new files, commit with a message, and push to a Git remote.
#!/bin/bash
# --- Configuration ---
REPO_PATH="/path/to/your/git/repo"
COMMIT_MESSAGE="Automated update $(date +"%Y-%m-%d %H:%M:%S")"
REMOTE="origin"
BRANCH="main"
# --- Script Logic ---
if [ ! -d "$REPO_PATH" ]; then
echo "Error: Repository path '$REPO_PATH' not found." >&2
exit 1
fi
cd "$REPO_PATH" || { echo "Failed to change directory to $REPO_PATH"; exit 1; }
echo "Updating Git repository at $REPO_PATH..."
# Pull the latest changes from the remote
echo "Pulling latest changes from $REMOTE/$BRANCH..."
git pull "$REMOTE" "$BRANCH"
if [ $? -ne 0 ]; then echo "Git pull failed." >&2; exit 1; fi
# Add all new or modified files
echo "Adding all changes..."
git add .
# Check if there are any changes to commit
if git diff-index --quiet HEAD --;
then
echo "No changes to commit."
else
# Commit changes
echo "Committing changes with message: '$COMMIT_MESSAGE'"
git commit -m "$COMMIT_MESSAGE"
if [ $? -ne 0 ]; then echo "Git commit failed." >&2; exit 1; fi
# Push changes to the remote
echo "Pushing changes to $REMOTE/$BRANCH..."
git push "$REMOTE" "$BRANCH"
if [ $? -ne 0 ]; then echo "Git push failed." >&2; exit 1; fi
echo "Repository updated and pushed successfully."
fi
echo "Git automation script finished."
How it works: This script automates the routine Git operations of pulling, adding all changes, committing, and pushing. It navigates to the specified repository, pulls the latest updates from the remote, stages all modified and new files, commits them with a predefined message (including a timestamp), and then pushes these changes to the remote branch. This is highly useful for CI/CD pipelines, automated deployments, or regular synchronization of development environments.