BASH
Automate Git Pull Across Multiple Repositories
Learn how to write a bash script to iterate through a directory of Git repositories and perform a 'git pull' on each, keeping all local branches up-to-date efficiently.
#!/bin/bash
# Directory containing your Git repositories
REPOS_DIR="/path/to/your/projects"
echo "Starting Git pull for repositories in $REPOS_DIR..."
# Loop through all subdirectories in REPOS_DIR
for repo in "$REPOS_DIR"/*/; do
# Check if it's a directory and a Git repository
if [ -d "$repo" ] && git -C "$repo" rev-parse --is-inside-work-tree &>/dev/null; then
echo -e "
--- Pulling updates for: $(basename "$repo") ---"
(cd "$repo" && git pull)
if [ $? -ne 0 ]; then
echo "Error pulling updates for $(basename "$repo")"
fi
fi
done
echo -e "
All Git repositories updated."
How it works: This script automates the process of updating multiple Git repositories. It iterates through all subdirectories within a specified `REPOS_DIR`. For each subdirectory, it first checks if it's a valid directory and a Git repository using `git -C "$repo" rev-parse --is-inside-work-tree`. If it is, the script navigates into that directory using `cd "$repo"` within a subshell `()` and executes `git pull` to fetch and merge the latest changes from the remote. Basic error handling is included to report issues during a pull operation.