git ignore .env files not working

GitLaravelLaravel 5.2Gitlab

Git Problem Overview


I have a laravel 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 Gitlab.

Any ideas what could be causing this?

Thanks for your help!

Git Solutions


Solution 1 - Git

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 - Git

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 - Git

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

Solution 4 - Git

You can remove that file from cached.

try below command

git rm .env --cached
git commit -m "Any message"

Solution 5 - Git

No need to use clear cache command, there's much simpler solution also doable in GUI.

If you are absolutely sure, your .gitignore rule is correct:

  1. stage the file that is not being ignored (STAGE ONLY DON'T COMMIT)
  2. unstage the file
  3. The file should not appear in changes anymore

Solution 6 - Git

The best solution is to put the .env file in it's own folder, then add that folder to the .gitignore file. example:

  • create a folder called "vars"
  • move your .env file into that folder
  • add the vars folder in .gitignore
*inside .gitignore*

/node_modules
/vars

-Make sure you add the path to dotenv.config() in your main file where you use the enviroment variables

dotenv.config({path: "./vars/.env"}

Then you can continue to reference you environment variables the same way as before

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMattView Question on Stackoverflow
Solution 1 - GitBryce DrewView Answer on Stackoverflow
Solution 2 - GitChirag IngdeView Answer on Stackoverflow
Solution 3 - Githamed dehghanView Answer on Stackoverflow
Solution 4 - GitManjeet Kumar NaiView Answer on Stackoverflow
Solution 5 - Gitjave.webView Answer on Stackoverflow
Solution 6 - GitKwanele GamedzeView Answer on Stackoverflow