BASH
Automate Updating Multiple Git Repositories
A Bash script to efficiently navigate through multiple Git repositories within a parent directory and pull the latest changes, streamlining your development workflow.
#!/bin/bash
# Script to update all Git repositories in the current directory
PARENT_DIR="." # Or specify a different path, e.g., "/path/to/projects"
echo "Updating Git repositories in $PARENT_DIR..."
for dir in "$PARENT_DIR"/*/; do
if [ -d "$dir/.git" ]; then
echo -e "
--- Entering $(basename "$dir") ---"
(cd "$dir" && git pull --rebase)
if [ $? -eq 0 ]; then
echo "$(basename "$dir"): Successfully pulled latest changes."
else
echo "$(basename "$dir"): Failed to pull latest changes. Please check manually."
fi
fi
done
echo -e "
All repositories checked."
How it works: This script iterates through all subdirectories within a specified parent directory. For each subdirectory, it checks if it's a Git repository by looking for a `.git` folder. If it is, the script changes into that directory and executes `git pull --rebase` to fetch and integrate the latest changes while keeping a clean commit history. Error handling is included.