BASH

Batch Git Pull for Multiple Local Repositories

Keep multiple local Git repositories up-to-date with a single Bash script that iterates through directories and performs a `git pull` on each, saving manual effort.

#!/bin/bash
# Configuration
PARENT_DIR="/home/youruser/projects" # Parent directory containing your Git repos

echo "Updating all Git repositories under $PARENT_DIR..."

# Loop through each subdirectory in the PARENT_DIR
for repo_dir in "$PARENT_DIR"/*/; do
    if [ -d "$repo_dir/.git" ]; then
        echo -e "
--- Entering $(basename "$repo_dir") ---"
        cd "$repo_dir" || { echo "Failed to change directory to $repo_dir"; continue; }
        git pull
        if [ $? -ne 0 ]; then
            echo "Error pulling changes in $(basename "$repo_dir")"
        fi
        cd - > /dev/null # Go back to the original directory quietly
    else
        echo "Skipping $(basename "$repo_dir"): Not a Git repository."
    fi
done

echo -e "
All specified Git repositories updated."
How it works: This script automates the process of updating multiple local Git repositories. It iterates through all subdirectories within a specified `PARENT_DIR`. For each subdirectory, it checks if it's a Git repository (by looking for the `.git` directory). If it is, the script navigates into that directory, executes `git pull` to fetch and merge latest changes, and then returns to the parent directory. This is a common workflow for developers managing several projects concurrently.

Need help integrating this into your project?

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

Hire DigitalCodeLabs