BASH
Automating Git Repository Setup and Updates
Streamline development workflows by automating Git cloning, pulling, and dependency installation for new or existing projects.
#!/bin/bash
# --- Configuration ---
REPO_URL="https://github.com/your-org/your-repo.git"
PROJECT_DIR="your-repo-name"
# --- Functions ---
clone_and_setup() {
echo "Cloning repository: $REPO_URL into $PROJECT_DIR"
git clone "$REPO_URL" "$PROJECT_DIR"
if [ $? -ne 0 ]; then
echo "Failed to clone repository. Exiting."
exit 1
fi
cd "$PROJECT_DIR" || { echo "Failed to change directory."; exit 1; }
echo "Installing dependencies (npm install)..."
npm install
if [ $? -ne 0 ]; then
echo "Failed to install npm dependencies."
exit 1
}
echo "Project setup complete for $PROJECT_DIR."
}
update_project() {
if [ ! -d "$PROJECT_DIR" ]; then
echo "Project directory '$PROJECT_DIR' not found. Please run setup first."
exit 1
}
cd "$PROJECT_DIR" || { echo "Failed to change directory."; exit 1; }
echo "Pulling latest changes from Git..."
git pull origin main # Or your default branch
if [ $? -ne 0 ]; then
echo "Failed to pull latest changes."
exit 1
}
echo "Installing/updating dependencies (npm install)..."
npm install
if [ $? -ne 0 ]; then
echo "Failed to install/update npm dependencies."
exit 1
}
echo "Project updated for $PROJECT_DIR."
}
# --- Main Execution ---
if [ -d "$PROJECT_DIR" ]; then
read -p "Project directory '$PROJECT_DIR' already exists. Update it? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
update_project
else
echo "Operation cancelled."
exit 0
fi
else
read -p "Project directory '$PROJECT_DIR' does not exist. Set it up? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
clone_and_setup
else
echo "Operation cancelled."
exit 0
fi
fi
How it works: This Bash script automates the process of setting up a new Git repository or updating an existing one. It handles cloning the repository, navigating into the project directory, and installing Node.js dependencies (using `npm install`). The script intelligently detects if the project already exists and prompts the user to either set it up or update it, streamlining repetitive development tasks.