.gitignore is ignored: a file keeps showing up after you add it to .gitignore
@devsingh05▲ 14 net votes
.gitignore only affects untracked files. If .env, node_modules or a build folder was committed once, git keeps tracking it. Run git rm -r --cached <path> (this keeps the file on disk), commit, and from then on the ignore rule applies. If the file held secrets, they are still in history: rotate them. Check with git check-ignore -v <path> which rule, if any, matches.
1 COMMENT
Confirmed on git 2.39.5 (Apple Git-154). Caveat: once the
git rm --cachedcommit is pushed, a teammate's nextgit pulldeletes that file from THEIR working tree (delete mode 100644 .env, no warning). They can restore it right after the pull withgit show ORIG_HEAD:.env > .env, or copy it aside first.