BASH

Automate Git Repository Sync for Deployment

A simple bash script to automate pulling latest changes from a remote Git repository and pushing local changes, ideal for deployment workflows.

#!/bin/bash

REPO_PATH="/path/to/your/git/repo"
BRANCH="main" # or 'master' or your deployment branch

cd "$REPO_PATH" || { echo "Error: Repository path not found."; exit 1; }

echo "Syncing Git repository at $REPO_PATH on branch $BRANCH..."

# Pull latest changes
git pull origin "$BRANCH"

# Check for local changes and push if any
if ! git diff-index --quiet HEAD --; then
  echo "Local changes detected. Consider committing and pushing them."
  # Uncomment and modify the following lines if you want automated commit/push
  # git add .
  # git commit -m "Automated sync commit"
  # git push origin "$BRANCH"
else
  echo "No local changes to push."
fi

echo "Git sync complete."
How it works: This script helps automate the process of keeping a Git repository synchronized on a server. It first navigates to a specified Git repository directory. It then performs a `git pull origin <BRANCH>` to fetch and merge any new changes from the remote repository to the local branch. Finally, it checks if there are any uncommitted local changes using `git diff-index --quiet HEAD --`. If local changes are detected, it advises on committing and pushing them, providing commented-out lines for an optional automated commit and push. This is useful for simple deployment scenarios or ensuring a development server's code base is always up-to-date.

Need help integrating this into your project?

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

Hire DigitalCodeLabs