This Bash script performs recursive checks on all subdirectories of...

July 4, 2025 at 09:51 PM

#!/bin/bash # Define the base directory BASE_DIR="$HOME/Repos" # Check if the base directory exists if [ ! -d "$BASE_DIR" ]; then echo "Error: Directory $BASE_DIR does not exist." exit 1 fi # Function to recursively find and check git repositories check_git_repos() { local current_dir="$1" # Iterate over all items in the current directory for item in "$current_dir"/*; do if [ -d "$item" ]; then # If it's a directory, check if it's a Git repository if [ -d "$item/.git" ]; then echo "--------------------------------------------------" echo "Checking Git repository: $(basename "$item")" echo "Path: $item" ( cd "$item" || exit 1 # Change to the repository directory # Check for local changes if git diff --quiet && git diff --cached --quiet; then echo "No local changes detected. Pulling latest changes..." git pull else echo "Local changes detected. Skipping pull." fi git status # Always show status after checking/pulling ) echo "--------------------------------------------------" else # If it's not a Git repository, recurse into it check_git_repos "$item" fi fi done } echo "Starting Git status and pull check in $BASE_DIR and its subdirectories..." check_git_repos "$BASE_DIR" echo "Git status and pull check complete."

This Bash script performs recursive checks on all subdirectories of a specified base directory to identify Git repositories and pull the latest changes from their remote repositories if no local changes exist. Here's a step-by-step explanation of what it does:

  1. Set the Base Directory:
    The BASE_DIR variable is set to $HOME/Repos (default directory to begin scanning for Git repositories).

  2. Verify Base Directory:
    The script checks if the specified BASE_DIR exists. If it does not, it exits with an error message.

  3. Define Recursive Function check_git_repos:

    • This function takes a directory path as an argument and iterates over all items in that directory.
    • For each item:
      • If the item is a directory and contains a .git subdirectory, it's identified as a Git repository.
        • The script then:
          1. Outputs the repository name and path.
          2. Changes the working directory to the repository.
          3. Uses git diff to check for local unstaged or staged changes:
            • If no changes are detected, it runs git pull to fetch and merge the latest changes from the remote repository.
            • If changes are detected, it skips the git pull and avoids disrupting local work.
          4. Runs git status to display the current state of the repository.
      • If the item is not a Git repository but still a directory, the function recurses into that directory to repeat the checks.
  4. Run the Function on BASE_DIR:
    After defining the function, the script invokes it on BASE_DIR, recursively checking its subdirectories for Git repositories.

  5. Output Status:
    The script prints messages to indicate progress or issues (e.g., identifying repositories, detecting local changes, skipping pulls, etc.) and notifies the user upon completion.

Use Case:

This script is useful for managing multiple Git repositories under a parent directory, ensuring they are up-to-date with their remote changes without disrupting ongoing work if local changes are present. It's especially handy for developers working with many projects simultaneously.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node