Demo, all content is generated
Question

git push rejected: GH013 Repository rule violations, Push cannot contain secrets

Solved · 523 views · asked by kaito · edited

Trying to push and I get

remote: error: GH013: Repository rule violations found for refs/heads/main.
remote: - GITHUB PUSH PROTECTION
remote:   Push cannot contain secrets
remote:   —— Stripe API Key ——
remote:     locations:
remote:       - commit: 8f3c2a1
remote:         path: .env:4

I deleted the .env file and committed again but it still refuses. It's 4 commits back.

What I’ve tried

Deleted .env and committed. Added .env to .gitignore. Cursor suggested clicking the link to 'allow the secret' but that sounds wrong.

Comment
Don't click the allow link. Writing up the steps. chidi_eze · edited

2 answers

Marked as helpful by the asker
chidi_eze · edited

Don't click 'allow'. The key is still in commit 8f3c2a1; deleting the file in a new commit doesn't remove it from history, and push sends all 4 commits.

Since nothing was pushed yet, rewrite the local commits:

git rm --cached .env          # if it's still tracked
echo .env >> .gitignore
git rebase -i HEAD~5          # mark 8f3c2a1 as 'edit'
# at that stop:
git rm --cached .env
git commit --amend --no-edit
git rebase --continue

Then push. Good news: push protection blocked it, so the key never reached GitHub. If you've never pushed this repo anywhere else, you don't strictly need to rotate the Stripe key. If in doubt, rotate, it's two minutes in the Stripe dashboard.

Comment
Rebase worked after one conflict. Pushed. I rotated anyway, better safe. kaito · edited
Interactive rebase in the terminal is scary the first time. If that's the case, the 'soft reset' route works too: git reset --soft origin/main, unstage .env, commit everything again as one commit. katja_s · edited
lena_ops · edited

And add a pre-commit secret scanner (gitleaks) or at least a global gitignore with .env* so it can't happen in the next project either.

Comment