Created
September 3, 2025 06:05
-
-
Save balexandre/02b086a433b5cb396fcddff69c6ddf54 to your computer and use it in GitHub Desktop.
Recursively update all local repositories in directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Define the parent directory where all your Git repositories are located | |
| # Run "pwd" in your terminal to find the correct path | |
| GIT_REPOS_PARENT_DIR="/Users/balexandre/Repositories" | |
| # Check if the directory exists | |
| if [ ! -d "$GIT_REPOS_PARENT_DIR" ]; then | |
| echo "Error: Directory '$GIT_REPOS_PARENT_DIR' not found." | |
| exit 1 | |
| fi | |
| echo "🤩 Starting Git repository updates in $GIT_REPOS_PARENT_DIR..." | |
| CUR_LEN=0 | |
| DIR_LEN=$(find "$GIT_REPOS_PARENT_DIR" -mindepth 1 -maxdepth 1 -type d | wc -l | xargs) | |
| if [ "$DIR_LEN" -eq 0 ]; then | |
| echo "No Git repositories found in $GIT_REPOS_PARENT_DIR." | |
| exit 1 | |
| fi | |
| echo "🤩 Found $DIR_LEN Git repositories" | |
| # Loop through each subfolder in the parent directory | |
| for repo_dir in "$GIT_REPOS_PARENT_DIR"/*/; do | |
| CUR_LEN=$((CUR_LEN + 1)) | |
| # Check if it's a Git repository | |
| if [ -d "$repo_dir/.git" ]; then | |
| repo_name=$(basename "$repo_dir") | |
| echo -e "\n--- ⭐️ Entering $repo_name ($CUR_LEN/$DIR_LEN) ---" | |
| # Navigate into the repository directory | |
| cd "$repo_dir" || { echo "Failed to change directory to $repo_dir"; continue; } | |
| # Perform Git operations | |
| echo "Checking out develop branch..." | |
| git checkout develop || git checkout master || git checkout main || { echo "Failed to checkout develop in $repo_name"; continue; } | |
| echo "Fetching --all --prune..." | |
| git fetch --all --prune || { echo "Failed to fetch --all --prune in $repo_name"; continue; } | |
| echo "Pulling latest changes..." | |
| git pull || { echo "Failed to pull in $repo_name"; continue; } | |
| echo "--- 🏁 Finished $repo_name ---" | |
| fi | |
| done | |
| echo -e "\n🥳 All Git repositories updated." | |
| # run the command below to make this script executable | |
| # chmod +x ./update_git_repos.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment