Search code examples
gitgithubgitignore

Ignoring .env files in my project with .gitignore


I'm doing a project in JavaScript (React / Node) and I want to ignore every .env files in it,

I don't only have one .env file at the root of the project, because I have a folder for the front-end and another for the back-end,

Root
  backend
    .env
  frontend
    .env
  .gitignore

And in the .gitignore file :

/node_modules
/backend/node_modules
/frontend/node_modules
/backend/.env
/frontend/.env

node_modules folders are ignored but not the env files,

The gitignore file is at the root, but should I have a gitignore file in every folder ?...


Solution

  • Solution:

    **/.env
    

    Explanation:

    A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".

    https://git-scm.com/docs/gitignore