BASH
Batch Git Pull Across Multiple Repositories
Streamline your workflow by automating the process of pulling the latest changes from multiple Git repositories within a specified root directory, ideal for monorepos or related projects.
#!/bin/bash
# Define the root directory where your Git repositories are located
# Change this to your actual parent directory for projects
ROOT_DIR="$HOME/my_projects"
# Check if the root directory exists
if [ ! -d "$ROOT_DIR" ]; then
echo "Error: Root directory '$ROOT_DIR' not found."
exit 1
fi
echo "Starting batch Git pull in '$ROOT_DIR'..."
# Find all .git directories and go to their parent (the repo root)
find "$ROOT_DIR" -type d -name ".git" | while IFS= read -r git_dir;
do
# Get the parent directory of the .git folder (which is the repository root)
repo_dir=$(dirname "$git_dir")
repo_name=$(basename "$repo_dir")
echo "
--- Processing repository: $repo_name ---"
cd "$repo_dir" || {
echo "Failed to change directory to $repo_dir. Skipping."
continue
}
# Check current branch and pull
current_branch=$(git rev-parse --abbrev-ref HEAD)
echo "Current branch: $current_branch"
# Perform git pull
git pull origin "$current_branch" || {
echo "Error pulling $repo_name. Please resolve conflicts or issues manually."
}
done
echo "
Batch Git pull complete."
How it works: This script automates the process of updating multiple Git repositories. It defines a `ROOT_DIR` where your projects are located, then uses `find` to locate all `.git` subdirectories. For each found `.git` directory, it changes into its parent directory (the repository root), determines the current branch, and executes `git pull origin <current_branch>`. This is highly useful for developers managing several related projects or working within a monorepo structure, ensuring all local branches are up-to-date with their respective remotes.