Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

An API key or .env file was committed to a Git repository

Errorremote: error: GH013: Repository rule violations found for refs/heads/main. (a push blocked because it contains a secret)

Short answer

Treat the secret as stolen the moment it was pushed. Revoke it and issue a new one first, because deleting history does not undo exposure. Then stop tracking the file, rewrite history with git filter-repo in a fresh clone, force-push, ask GitHub Support to clear cached copies, and turn on secret scanning with push protection.

  • Applies to Git 2.47 and newer, git-filter-repo 2.47 and newer, GitHub.com
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out what was committed, in which commits, and whether it was ever pushed. Search every branch for the value or the file, without printing the secret into shared logs.

    git log --all --oneline -- .env
    git log --all --oneline -S'PART_OF_THE_KEY' | head
    git branch -r --contains $(git log --all --format=%H -n 1 -- .env)
  2. 2Match what you see, then jump to its fix

    1. 1The key was in any commit that reached a remote, a fork, a package or a build log, even briefly.The secret is still valid and has been exposedFix
    2. 2git ls-files lists .env (or a key file), and git status keeps showing it as modified.The file is tracked and nothing tells Git to ignore itFix
    3. 3git log --all -- .env or the -S search still finds commits after you removed the file in a new commit.The secret is still in older commits, branches and tagsFix
    4. 4After the force-push the commit is still reachable by URL or through a pull request, or a collaborator pushes the old history back.Copies remain: pull requests, forks, caches and other clonesFix
    5. 5This is the second time, or secret scanning alerts exist that nobody triaged.Nothing stops the next secret from being pushedFix

What does the error look like?

  • remote: error: GH013: Repository rule violations found for refs/heads/main. (a push blocked because it contains a secret)
  • GitHub secret scanning alert: a valid secret was detected in a commit
  • A provider email saying your API key was found in a public repository
  • .env shows up in git log --stat or in the repository's file list

Cause 1 of 5

The secret is still valid and has been exposed

1Check

Assume that anything pushed to a shared or public remote has been copied. Check the provider's usage or audit log for calls you do not recognise, and note which systems (databases, cloud accounts, payment tools) the key could reach.

git log --all --format="%h %an %ad %s" -S'PART_OF_THE_KEY' | head
git remote -v

2Fix

Revoke the secret at its provider and create a new one before touching the repository. Put the new value in the app's real secret store or environment, redeploy, and review the audit log for use you did not make. Removing the file from history afterwards is cleanup, not the cure.

# 1. at the provider: revoke or rotate the key, then check its access log
# 2. update the deployed secret store / environment variable
# 3. only then clean the repository (next causes)

3Verify

The old key is rejected by the provider, the app works with the new one, and no unknown activity appears in the audit log.

curl -s -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer OLD_KEY" https://api.provider.example/v1/me   # expect 401 or 403

Confirmed by docs.github.com

Did this fix it?

Cause 2 of 5

The file is tracked and nothing tells Git to ignore it

1Check

An ignore rule only affects files Git does not already track. Once a file is committed, adding it to .gitignore changes nothing for that file.

git ls-files | grep -E '(^|/)\.env($|\.)|\.pem$|id_rsa|id_ed25519'
git check-ignore -v .env

2Fix

Stop tracking the file but keep it on disk, add ignore rules, and commit a harmless template instead so teammates know which variables exist.

git rm --cached .env
printf '.env\n.env.*\n!.env.example\n' >> .gitignore
cp .env .env.example && sed -i.bak 's/=.*/=/' .env.example && rm .env.example.bak
git add .gitignore .env.example
git commit -m "Stop tracking .env and add template"

3Verify

git ls-files no longer lists .env and git status is clean for it.

git ls-files | grep -cE '(^|/)\.env$'    # expect 0
git check-ignore -v .env

Confirmed by git-scm.com

Did this fix it?

Cause 3 of 5

The secret is still in older commits, branches and tags

1Check

Deleting a file in a new commit leaves it in every earlier commit. Rewriting history needs a tool; git filter-repo is the one Git's own documentation points to for this. Work in a fresh clone because the rewrite is destructive and the tool refuses to run in anything else.

git filter-repo --version
git clone --mirror git@github.com:you/repo.git repo-clean.git

2Fix

In a fresh clone, remove the file from all history, or replace the secret text wherever it appears. GitHub's guide uses the --sensitive-data-removal flag, which collects the details needed for cleanup of other copies. Check the result, then push every ref. filter-repo removes the origin remote on purpose, so add it back first.

git clone git@github.com:you/repo.git repo-clean && cd repo-clean

# remove a whole file from every commit
git filter-repo --sensitive-data-removal --invert-paths --path .env

# or replace specific strings (one per line; ==> picks the replacement text)
printf 'literal:sk_live_THE_SECRET==>REMOVED\n' > ../replacements.txt
git filter-repo --sensitive-data-removal --replace-text ../replacements.txt

git remote add origin git@github.com:you/repo.git
git push --force --mirror origin

3Verify

A search over every ref finds nothing, in the clean clone and in a new clone of the remote.

git log --all --oneline -S'PART_OF_THE_KEY' | wc -l    # expect 0
git log --all --oneline -- .env | wc -l                # expect 0

Confirmed by github.com

Did this fix it?

Cause 4 of 5

Copies remain: pull requests, forks, caches and other clones

1Check

GitHub keeps pull request refs, which cannot be overwritten by a push, and cached views of old commits; forks and other clones keep their own copy. GitHub's guide counts the affected pull requests from the changed refs file filter-repo leaves.

grep -c '^refs/pull/.*/head$' .git/filter-repo/changed-refs

2Fix

Open a request with GitHub Support with the repository name, the number of affected pull requests and the first changed commits from the tool's output, so they can clear cached views and pull request refs. Tell collaborators to re-clone, not to pull or merge, and handle forks with their owners. Rotation from the first cause is what makes all remaining copies harmless.

# collaborators, in a new folder
git clone git@github.com:you/repo.git

3Verify

Old commit links return 404 after support confirms the cleanup, and the secret in them was already revoked.

git ls-remote origin | wc -l

Confirmed by docs.github.com

Did this fix it?

Cause 5 of 5

Nothing stops the next secret from being pushed

1Check

On GitHub, push protection scans a push for supported secret patterns and blocks it. User-level protection for public repositories is on by default; repository-level protection is off by default and is enabled by an administrator in the repository's security settings.

gh api repos/OWNER/REPO --jq '.security_and_analysis'

2Fix

Turn on secret scanning and push protection for the repository or organisation. If a push is blocked, remove the secret instead of choosing the bypass reason; a bypass opens or closes an alert depending on the reason you give, and leaves the value in history. Keep real values only in the environment or a secret manager.

# Settings > Security > Advanced Security (names vary by plan):
# enable Secret scanning and Push protection

3Verify

A test push containing a fake supported token is refused with a message that explains why.

gh api repos/OWNER/REPO --jq '.security_and_analysis'

Confirmed by docs.github.com

Did this fix it?

How do I stop it coming back?

  • Keep .env, key and certificate files in .gitignore from the first commit, and commit only an .env.example without values.
  • Load secrets from the environment or a secret manager; never hard-code them in source or Dockerfiles.
  • Turn on secret scanning and push protection for every repository and organisation you own.
  • Rotate any key that appears in a screenshot, log or chat the same way, and give each key the least access it needs.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.