BASH

Update All Git Repositories in a Directory

Automate updating multiple Git repositories within a specified parent directory using a bash script, streamlining development workflows for microservices or multi-project setups.

#!/bin/bash

# Configuration
PARENT_DIR="/home/user/projects" # Directory containing your Git repositories

# Check if the parent directory exists
if [ ! -d "$PARENT_DIR" ]; then
  echo "Error: Parent directory '$PARENT_DIR' not found."
  exit 1
fi

echo "Updating all Git repositories in '$PARENT_DIR'..."

# Loop through each subdirectory in PARENT_DIR
for repo_dir in "$PARENT_DIR"/*/; do
  # Check if it's a Git repository
  if [ -d "$repo_dir/.git" ]; then
    echo "
--- Updating repository: $(basename "$repo_dir") ---"
    (cd "$repo_dir" && git pull --ff-only)
    if [ $? -ne 0 ]; then
      echo "Error updating $(basename "$repo_dir")."
    fi
  fi
done

echo "
All Git repositories update attempt completed."
How it works: This script simplifies managing multiple Git repositories. It iterates through all subdirectories within a 'PARENT_DIR'. For each subdirectory, it checks if it contains a '.git' directory, indicating a Git repository. If so, it navigates into that directory ('cd "$repo_dir"') and executes 'git pull --ff-only' to fetch and fast-forward merge changes from the remote, providing a quick way to update all local project copies.

Need help integrating this into your project?

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

Hire DigitalCodeLabs