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:
Want to master this scenario in a live sandbox? KodeKloud's Advanced Git Workflows, Rebase & Interactive Diagnostics covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: "In a fast-paced team with dozens of pull requests merged daily, Git workflow hygiene was critical. The interviewer is testing: Using Git diff for selective CI, path-based triggering.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third."
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...
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
-
Running raw git push --force rather than git push --force-with-lease on shared team feature branches when configuring monorepo slow every
git push --forceunconditionally overwrites remote commits made by teammates, whereas--force-with-leaseaborts if the remote ref was updated. - Resolving complex merge conflicts with blind checkout overwrite strategies without running unit tests when configuring monorepo slow every Unilateral conflict resolution frequently clobbers complementary logic introduced in parallel feature branches, creating regression bugs.
-
Discarding corrupted working directories prematurely instead of recovering lost state using git reflog when configuring monorepo slow every
git reflogtracks every HEAD movement, allowing instant recovery of discarded commits, botched rebases, or deleted local branches. - Disabling branch protection rules and mandatory PR approval reviews on shared release branches when configuring monorepo slow every Without branch protection, developers can bypass CI validation gates and push untested code directly to production branches.