{"slug": "ai-wrote-your-env-into-a-commit-what-github-push-protection-catches-and-why-won", "title": "AI wrote your .env into a commit? What GitHub push protection catches, and why `.gitignore` alone won't save you", "summary": "A developer demonstrated that adding a .env file to .gitignore and running git rm --cached does not remove a committed API key from Git history, since git log --all -- .env still surfaces the commit that introduced it. The writeup notes that GitHub's user-level push protection is enabled by default and blocks secrets from being pushed to public repositories on GitHub.com, while repository-level push protection is off by default and requires GitHub Secret Protection. The developer advises treating any key that reached a pushed commit as exposed and rotating it with the provider rather than simply deleting the file.", "body_md": "**Result first:** I committed a fake API key in a `.env` file, then \"fixed\" it the way most people do: add `.env` to `.gitignore` and run `git rm --cached .env`. The file stopped being tracked, **but the key was still in history.** `git log --all -- .env` still lists the commit that added it. Anyone who can clone the repository can read that commit.\n\n**Where GitHub helps (from GitHub Docs):**\n\n- \n**Push protection for users** is \"enabled by default.\" It \"stops you from pushing secrets to public repositories on GitHub.\" It is available only on GitHub.com.\n- It scans pushes from the command line, commits made in the GitHub UI, file uploads, REST API requests, and (on public repos only) interactions with the GitHub MCP server.\n- When it blocks you, you get a message explaining why. You remove the secret and push again.\n- \n**Push protection for repositories** is a separate feature. It is off by default and requires GitHub Secret Protection to be enabled.\n- To check your own setting: profile picture → **Settings** →**Code security** → under \"User\",**Push protection for yourself** .\n\n**What this means for AI-assisted coding:**\n\n1. Don't let an agent create files that hold keys inside the working tree it commits from. Put `.env` in`.gitignore`*before* the first commit.\n2. Push protection only knows the secret types it supports. It is a safety net, not a guarantee, and a private repository doesn't get the user-level protection.\n3. If a real key ever reached a pushed commit, treat the key as exposed. Replace it with your provider; don't just delete the file.\n\n## \n  \n  \n  Sources\n\n*From Penloom Studio's free AI lessons: [penloomstudio.com](https://penloomstudio.com/landing-cogloom-motion.html)*", "url": "https://wpnews.pro/news/ai-wrote-your-env-into-a-commit-what-github-push-protection-catches-and-why-won", "canonical_source": "https://dev.to/penloom_studio_829b7817d3/ai-wrote-your-env-into-a-commit-what-github-push-protection-catches-and-why-gitignore-alone-9i9", "published_at": "2026-10-22 15:35:39+00:00", "updated_at": "2026-10-22 15:44:32.055308+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools", "ai-agents"], "entities": ["GitHub", "GitHub Secret Protection", "GitHub MCP server", "Penloom Studio"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/ai-wrote-your-env-into-a-commit-what-github-push-protection-catches-and-why-won", "markdown": "https://wpnews.pro/news/ai-wrote-your-env-into-a-commit-what-github-push-protection-catches-and-why-won.md", "text": "https://wpnews.pro/news/ai-wrote-your-env-into-a-commit-what-github-push-protection-catches-and-why-won.txt", "jsonld": "https://wpnews.pro/news/ai-wrote-your-env-into-a-commit-what-github-push-protection-catches-and-why-won.jsonld"}}