A Secret Got Committed. Now What?

A Secret Got Committed. Now What?

18 October 2026 · By Laravel Git · 2 min read

It happens: .env, an API key, a database password ends up in a commit. The fix is the same every time, and the order matters.

1. Rotate the secret first#

Deleting the file from git does nothing while the key still works. Assume anything committed is public, even in a private repo: clones, forks and CI caches all copy it. Revoke or rotate the credential at the provider before touching the repository.

2. Scrub the history#

Once the old key is dead, remove it from history so the repo stops carrying it. git filter-repo is the tool that works:

pip install git-filter-repo
git filter-repo --path .env --invert-paths

This rewrites every commit, so force-push and have the team re-clone. A commit hash they still hold can resurrect the file.

3. Stop it happening again#

  • .gitignore the file and commit a .env.example with the keys but no values.
  • Laravel reads real values from the environment, so nothing secret needs to be in the repo at all.
  • Turn on your host’s push protection or run a pre-commit scan so a key gets caught before it lands.

The short version#

A scrubbed repo with a live key is still a breach. A rotated key with an ugly history is just housekeeping. Rotate first.

Share this post

Our network