gitgitignore

git is not respecting gitignore instruction to ignore .env files


I have a project. In the root directory are these 4 files:

.env .env.example .env.local .env.staging

I have a .gitignore file, and I'm listing these 4 files in the .gitignore, one after another, like this

.env
.env.example
.env.local
.env.staging

My git repository does not contain .env or .env.example, but it DOES contain .env.local and .env.staging. I've tried everything I can think of, but it keeps syncing these 2 files with git.

Any ideas what could be causing this?


Solution

  • Use git rm:

    If you have already added the files to be tracked, you need to remove them from tracking:

    git rm .env.local --cached
    git rm .env.staging --cached
    git commit -m "Stopped tracking env.local, and env.staging"
    

    Now you should be able to clone your branch without those files being tracked.

    Note: Keep in mind that the contents of those files are in your history, and if they did contain sensitive data, then you need to completely remove that from history before putting it out there.