How do you stop secrets being committed to Git in the first place?

BeginnerScenarioPipelines at scale
Answer

Situation read → Catch it as early as possible: on the laptop, at push, and in CI, because each layer misses some cases.

What I'd do →

  1. Pre-commit hook with gitleaks:
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.21.2
    hooks:
      - id: gitleaks
  1. Push protection in GitHub (secret scanning) blocks pushes containing known token formats.
  2. CI scan of the whole history on each PR (gitleaks detect or TruffleHog), since hooks can be skipped.
  3. Remove the reason: use OIDC and runtime secret managers so there are fewer secrets for developers to handle at all.
  4. If one gets through: rotate first, then clean history.

Why → Layered checks catch what any single check misses, and pre-commit gives the fastest feedback.

Interview tip → Mention that pre-commit hooks are optional on the client, so server-side checks are required.

All Real-World Interview questions Roadmap: Interview readiness

More on Pipelines at scale