BASH

Manage Multiple Git Repositories (Pull All)

Keep all your local Git repositories up-to-date with a single bash command. This script recursively finds and pulls changes from all repos within a directory, streamlining development workflows.

#!/bin/bash

# Configuration
REPOS_ROOT_DIR="$HOME/Projects"

echo "Updating all Git repositories under $REPOS_ROOT_DIR..."

# Find all .git directories, then change to their parent and pull
find "$REPOS_ROOT_DIR" -type d -name ".git" | while read git_dir;
do
  REPO_DIR=$(dirname "$git_dir")
  echo "--> Entering $REPO_DIR"
  cd "$REPO_DIR" || {
    echo "Failed to change directory to $REPO_DIR. Skipping."
    continue
  }
  
  # Check if it's actually a git repo (to be safe)
  if git rev-parse --is-inside-work-tree > /dev/null 2>&1;
  then
    git pull --ff-only
    if [ $? -ne 0 ]; then
      echo "WARNING: Git pull failed for $REPO_DIR. Manual intervention might be needed."
    fi
  else
    echo "WARNING: $REPO_DIR is not a valid Git repository."
  fi

  echo "" # Newline for readability
done

echo "All repository updates attempted."
How it works: This script streamlines the process of updating multiple local Git repositories. It recursively searches a specified root directory for `.git` folders, navigates into each repository's root, and then performs a `git pull --ff-only` command to fetch and fast-forward merge changes from the remote. This is perfect for developers managing many projects, ensuring all their local branches are up-to-date with their respective remotes.

Need help integrating this into your project?

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

Hire DigitalCodeLabs