#!/usr/bin/env bash # Exit immediately if a command exits with a non-zero status, # or if an uninitialized variable is used. set -euo pipefail # Print usage instructions usage() { echo "Usage: $0 " echo "Example: $0 user@server.com /var/www/my-project production" exit 1 } # 1. Check arguments if [ "$#" -ne 3 ]; then echo "❌ Error: Invalid number of arguments." >&2 usage fi REMOTE_HOST="$1" REMOTE_PATH="$2" REMOTE_NAME="$3" # 2. Verify we are running inside a local Git repository if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then echo "❌ Error: This script must be run from within a local Git repository." >&2 exit 1 fi # Get current branch name (usually 'main' or 'master') CURRENT_BRANCH=$(git branch --show-current) if [ -z "$CURRENT_BRANCH" ]; then echo "❌ Error: Could not determine current branch. Is your repository empty?" >&2 exit 1 fi # 3. Check SSH connection echo "🔍 Checking SSH connection to ${REMOTE_HOST}..." if ! ssh -q -o BatchMode=yes -o ConnectTimeout=5 "$REMOTE_HOST" exit; then echo "❌ Error: Cannot establish SSH connection to ${REMOTE_HOST}." >&2 echo "Please check your SSH credentials, host address, or ssh-agent configuration." >&2 exit 1 fi # 4. Initialize and configure the remote repository over SSH echo "🚀 Creating remote directory and initializing Git on ${REMOTE_HOST}..." ssh "$REMOTE_HOST" bash -s <