An API key or .env file was committed to a Git repository
remote: 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.
Fix it fast
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)2Match what you see, then jump to its fix
- 1
The 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
git 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
git 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
After 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
This is the second time, or secret scanning alerts exist that nobody triaged.Nothing stops the next secret from being pushedFix
- 1
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 commitA 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 -v2Fix
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 403Confirmed by docs.github.com
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 .env2Fix
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 .envConfirmed by git-scm.com
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.git2Fix
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 origin3Verify
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 0Confirmed by github.com
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-refs2Fix
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.git3Verify
Old commit links return 404 after support confirms the cleanup, and the secret in them was already revoked.
git ls-remote origin | wc -lConfirmed by docs.github.com
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 protection3Verify
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
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
Security
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
Angular HttpClient does not add the X-XSRF-TOKEN header to a request
403 Forbidden on POST, PUT or DELETE while GET works
Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script.
5 causes, each with a fixSecurity
An [Authorize] endpoint rejects a request with a bearer token
Authorization Header sent: no Bearer token sent.
An [Authorize] endpoint without a policy name requires an authenticated user.
4 causes, each with a fixSecurity
ASP.NET Core API rejects an Angular user's token: the signature key was not found
401 Unauthorized
The API looked for the key named by the token's kid in the key set it downloaded from your Authority, and the key was not there.
4 causes, each with a fixSecurity
ASP.NET Core rejects the token before checking it: IDX12741, three segments expected
401 Unauthorized
IDX12741 means the string the API pulled out of the Authorization header is not shaped like a JWT: a signed token has three dot-separated parts and an encrypted one has five.
3 causes, each with a fixSecurity
A Microsoft Entra access token shows Invalid Signature when you check it yourself
Tokens that a Microsoft API receives might not always be a JWT that can be decoded
A token issued for Microsoft Graph is not meant for you to validate, so a failed signature check is expected.
2 causes, each with a fix