Skip to main content

Syncing Repositories

Overview

Check whether local branches are ahead of, behind, or in sync with their remote tracking branches across multiple repositories.

When to Use

Use when you need to verify if:

  • Any repository needs to be pushed (git push)
  • Any repository needs to be pulled (git pull)
  • All repositories are up-to-date with remote

Quick Reference

CommandPurpose
ls -d */Discover all repositories
git rev-listGet ahead/behind counts
Format: ahead behindFirst = local commits not pushed, Second = remote commits not pulled

Implementation

Step 1: Discover Repositories

ls -d */

Step 2: Fetch Remote Updates (Parallel)

For each repository, fetch latest remote references first:

cd repo-name && git fetch origin

This ensures accurate sync status by updating remote branch references.

Step 3: Check Sync Status (Parallel)

For each repository, check ahead/behind counts:

cd repo-name && git rev-list --left-right --count main...origin/main

Output format: ahead behind (e.g., 3 0 = 3 ahead, 0 behind)

Step 4: Present Results

Use a markdown table for clear presentation:

仓库状态领先落后
repo1✅ 已同步00
repo2⚠️ 需推送20
repo3⚠️ 需拉取01

Common Mistakes

MistakeWhy It's WrongFix
Not fetching before checkingUses stale remote references, misses remote changesAlways run git fetch first
Using git status -sb onlyDoesn't show ahead/behind counts clearlyUse git rev-list --left-right --count
Checking repos sequentiallySlow with many reposRun commands in parallel
Not handling missing remotesCommand fails or gives unclear outputAdd 2>/dev/null || echo "No remote"
Outputting raw numbersHard to parse mentallyFormat as readable table
Forgetting to cd into repoCommand runs in wrong directoryEach check needs cd repo &&

Real-World Impact

Before: 7 repos, manual git status in each, hard to compare

After: Single scan, clear table, immediate action items