git ignore .env files not working
Sebastian Wright
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.stagingMy 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!
6 Answers
Answer recommended by GitLabUse 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.
1Simply 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.
0It 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 You can remove that file from cached.
try below command
git rm .env --cached
git commit -m "Any message" 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:
- stage the file that is not being ignored (STAGE ONLY DON'T COMMIT)
- unstage the file
- The file should not appear in changes anymore
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
.envfile into that folder - add the
varsfolder 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