Git still tracks .env after adding it to .gitignore
You added .env to .gitignore, yet git status keeps listing it as modified. The ignore list is only read for files Git has never committed, so an already committed .env keeps being watched. The fix is to stop tracking it once, keep the file on disk, and commit that change.
Scope: a file that was already committed. For keeping keys out from the start see page 14, for erasing old commits see page 15, and for why deletion is not enough see page 16.
Why does Git still track .env after I add it to .gitignore?
Ignore rules only apply to untracked files. Run git rm --cached .env, then commit.
How do I confirm Git now ignores the file?
Run git check-ignore -v .env. It prints the rule and line that matches, and git status no longer lists the file as modified.
Tested commands and output
Command: git rm --cached .env
$ git status --short
M .env
?? .gitignore
$ git rm --cached .env
rm '.env'
$ git commit -am "stop tracking .env"
[main 7f3a8a2] stop tracking .env
1 file changed, 1 deletion(-)
delete mode 100644 .env
$ git status --short
?? .gitignore
$ git check-ignore -v .env
.gitignore:1:.env .envNote
The commit hash printed on your machine will differ from the one above. After the commit, other clones will see .env deleted on their next pull, so keep a copy of their own .env values.
Source: https://stackoverflow.com/questions/38983153/git-is-not-respecting-gitignore-instruction-to-ignore-env-files (Stack Overflow question this page answers) Source: https://git-scm.com/docs/gitignore (gitignore docs) Source: https://git-scm.com/docs/git-rm (git rm docs)
Full kit: Secrets Safety Kit