Q: Your monorepo CI is slow because every PR triggers tests for all 30 services. How would you use Git to determine which services are affected by a PR and only run their tests?
Use git diff to identify changed paths and map them to affected services:
🛠️ Production Runbook & Step-by-Step Resolution
Initial Diagnostics & Root Cause Analysis
Use git diff to identify changed paths and map them to affected services:
- Bazel/Pants/Buck — build systems that understand the dependency graph and test only affected targets:
bazel test //... --test_tag_filters=-manualwith remote caching. - Nx/Turborepo (JS/TS monorepos) —
nx affected --target=testautomatically determines what to test based on the Git diff. - git diff with
--diff-filter— distinguish added, modified, deleted, and renamed files for more precise mapping.
Remediation & Permanent Safeguards
Step 1: Get changed files in the PR: The three-dot syntax (...) finds the merge-base and shows only what the PR changed — not what main changed since the branch was created. Step 2: Map changed paths to services: This mapping can be a simple bash script, a JSON config file, or defined in CI config: Step 3: Handle shared dependencies: The tricky part. If libs/common/ changes, every service that depends on it must be tested. Maintain a dependency graph (or use build tools like Bazel, Nx, or Turborepo that understand it natively). Advanced approaches: The key insight: git diff --name-only gives you the raw data; the intelligence is in the mapping from file paths to services/test suites.
# Compare PR branch against the merge target:
CHANGED_FILES=$(git diff --name-only origin/main...HEAD)
- Bazel/Pants/Buck — build systems that understand the dependency graph and test only affected targ...
- Nx/Turborepo (JS/TS monorepos) — nx affected --target=test automatically determines what to test ...
- git diff with --diff-filter — distinguish added, modified, deleted, and renamed files for more pr...