Deleting .env in a new commit does not hide the key
Deleting a file in a new commit hides it from the latest snapshot only. Every earlier commit still holds the full content, and anyone who clones the repository can print it. The test below shows the file gone from disk while the old commit still returns the key.
Scope: showing that old commits keep the key. Stopping tracking is page 13, and actually removing it from history is page 15.
If I delete .env in a new commit, can people still see the key?
Yes. A delete or edit only changes the newest commit, so treat the key as leaked and revoke it.
Can I find which commits ever contained the file?
Yes. git log --oneline --all -- .env lists the commit that added it and the one that removed it.
Tested commands and output
Command: git show HEAD~1:.env
$ git rm -q .env
$ git commit -qm "remove env"
$ ls -A
.git
$ git show HEAD~1:.env
API_KEY=sk-test-FAKE123
$ git log --oneline --all -- .env
09ffe70 remove env
dfbd574 leakNote
Hashes on your machine will differ. Removing the file from history is covered on page 15.
Source: https://stackoverflow.com/questions/56336890/i-accidentally-uploaded-env-file-with-google-key-in-it-to-github-but-edited-the (Stack Overflow question this page answers) Source: https://git-scm.com/docs/git-log (git log docs) Source: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository (GitHub guide to removing sensitive data)
Full kit: Secrets Safety Kit