git ignore .env files not working

Solution 1:

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.

Solution 2:

Simply Run This Command.

git rm .env --cached
git commit -m "Stopped tracking .env File"

In laravel there are .env file include for connecting database. So for that you must remove .env file from cached.

Solution 3:

It is because your .env file has been pushed to git.

You should first delete that from git and push your changes.

rm -f .env
git add .
git commit -m "remove .env file from git"
git push