DEV Community

Cover image for Sync Git multiple repositories with shell/bash script
Vineeth.TR
Vineeth.TR

Posted on

5

Sync Git multiple repositories with shell/bash script

Managing multiple Git repositories can be a hassle, especially when we need to keep them up-to-date with the latest changes. Whether we're working on multiple projects or managing a large monorepo setup, manually cloning and pulling changes can be time-consuming. That's where git-sync.sh comes in!

In this post, I'll introduce git-sync.sh, a simple yet powerful shell / bash script that automates the process of cloning and pulling multiple Git repositories. This script not only saves you time but also provides clear feedback using emojis to indicate the success or failure of each operation.

Why Use git-sync.sh?

git-sync.sh is designed to:

  • Automate Repository Cloning: Check if a repository is already cloned and clone it if not.
  • Keep Repositories Up-to-Date: Pull the latest changes if the repository is already cloned.
  • Provide Clear Feedback: Use emojis to show the status of each operation, making it easy to see what happened at a glance.

How to Use git-sync.sh

1. Set Up the Script

First, you need to create the git-sync.sh file.

#!/bin/bash

# List of repositories to sync
REPOS=(
  "https://github.com/user/repo1.git"
  "https://github.com/user/repo2.git"
  "https://github.com/user/repo3.git"
)

# Directory where repositories will be cloned/pulled
TARGET_DIR="$HOME/projects"

# Emoji for status
CLONE="⬇️"
CLONE_SUCCESS="⬇️✅"
CLONE_FAILURE="⬇️❌"
PULL="🔄"
PULL_SUCCESS="🔄✅"
PULL_FAILURE="🔄❌"

# Ensure target directory exists
mkdir -p "$TARGET_DIR"

# Function to clone or pull a repository
sync_repo() {
  local repo_url=$1
  local repo_name=$(basename "$repo_url" .git)
  local repo_dir="$TARGET_DIR/$repo_name"

  # Check if the directory already exists
  if [ -d "$repo_dir" ]; then
    echo "$PULL Pull latest changes for  '$repo_name'"
    cd "$repo_dir" || { echo "$PULL_FAILURE Failed to enter directory '$repo_dir'"; return; }
    if git pull; then
      echo "$PULL_SUCCESS Successfully pulled latest changes for '$repo_name'"
    else
      echo "$PULL_FAILURE Failed to pull latest changes for '$repo_name'"
    fi
  else
    echo "$CLONE  Cloning '$repo_name' Repository"
    if git clone "$repo_url" "$repo_dir"; then
      echo "$CLONE_SUCCESS Successfully cloned '$repo_name'"
    else
      echo "$CLONE_FAILURE Failed to clone '$repo_name'"
    fi
  fi
}

# Sync each repository
for repo in "${REPOS[@]}"; do
  sync_repo "$repo"
done

Enter fullscreen mode Exit fullscreen mode

Make sure the script is executable:

chmod +x git-sync.sh
Enter fullscreen mode Exit fullscreen mode

2. Configure the Script

Edit git-sync.sh to include your list of repositories and specify the target directory:

# List of repositories to sync
REPOS=(
  "https://github.com/user/repo1.git"
  "https://github.com/user/repo2.git"
  "https://github.com/user/repo3.git"
)

# Directory where repositories will be cloned/pulled
TARGET_DIR="$HOME/projects"
Enter fullscreen mode Exit fullscreen mode

3. Run the Script

Now, simply run the script:

./git-sync.sh
Enter fullscreen mode Exit fullscreen mode

The script will automatically:

  1. Check if each repository in the list is already cloned in the target directory.
  2. Clone the repository if it's not present.
  3. Pull the latest changes if the repository is already cloned.

4. Status Feedback

The script provides status updates using emojis:

  • ⬇️: Cloning the repository.
  • ⬇️✅: Successfully cloned the repository.
  • ⬇️❌: Failed to clone the repository.
  • 🔄: Pull latest changes from the repository.
  • 🔄✅: Successfully pulled the latest changes.
  • 🔄❌: Failed to pull the latest changes.

Example Output

Here's an example of what the output might look like:

Repository 'repo1' already exists. Pulling latest changes...
🔄✅ Successfully pulled latest changes for 'repo1'

Repository 'repo2' not found. Cloning...
⬇️✅ Successfully cloned 'repo2'

Repository 'repo3' already exists. Pulling latest changes...
🔄❌ Failed to pull latest changes for 'repo3'
Enter fullscreen mode Exit fullscreen mode

Customization

You can customize git-sync.sh to fit your workflow:

  • Add or Remove Repositories: Modify the REPOS array to include your repositories.
  • Change Target Directory: Set TARGET_DIR to the desired location.
  • Customize Emojis: Personalize the emojis used for status feedback.

Troubleshooting

  • Permission Issues: Ensure the script is executable with chmod +x git-sync.sh.
  • Access Rights: Make sure you have the necessary SSH keys or credentials for private repositories.
  • Network Problems: Ensure you have a stable internet connection.

Happy coding! 👨‍💻🎉

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more