← Back to all snippets
BASH

Batch Git Pull Across Multiple Local Repositories

A convenient bash script to automate `git pull` across several local Git repositories, simplifying updates for multi-project development environments or local server setups.

#!/bin/bash

# Configuration
BASE_DIR="/home/user/projects"

echo "Starting Git pull for repositories under $BASE_DIR"

# Find all .git directories and navigate to their parent (the repo root)
find "$BASE_DIR" -maxdepth 3 -type d -name ".git" | while read GIT_DIR;
do
    REPO_DIR=$(dirname "$GIT_DIR")
    echo -e "
--- Pulling in $REPO_DIR ---"
    
    # Navigate to the repository directory
    if cd "$REPO_DIR"; then
        # Perform git pull
        git pull
        if [ $? -ne 0 ]; then
            echo "Error pulling changes in $REPO_DIR"
        fi
    else
        echo "Failed to change directory to $REPO_DIR"
    fi

    # Navigate back to the original directory (optional, or just let the loop continue)
    # Or, the subshell created by `find ... | while read ...` ensures `cd` only affects that subshell
    # and the main script's current directory is unchanged after the loop.
done

echo -e "
All repositories updated."
How it works: This script automates updating multiple Git repositories. It uses `find` to locate all `.git` directories within a specified base directory, then iterates through each one. For each found repository, it navigates into its parent directory and executes `git pull` to fetch and merge the latest changes from the remote. This is highly useful for developers working on many projects simultaneously or maintaining a local server with multiple Git-managed applications, saving time from manual updates.

Need help integrating this into your project?

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

Hire DigitalCodeLabs