使用 .gitignore 忽略我项目中的 .env 文件

Ignoring .env files in my project with .gitignore

我正在 JavaScript (React / Node) 中做一个项目,我想忽略其中的每个 .env 文件,

我的项目根目录下不只有一个.env文件,因为我有一个文件夹给前端,另一个给后端,

Root
  backend
    .env
  frontend
    .env
  .gitignore

并且在 .gitignore 文件中:

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

node_modules 文件夹被忽略但 env 文件不被忽略,

gitignore 文件在根目录下,但我应该在每个文件夹中都有一个 gitignore 文件吗?...

文件扩展名前加*即可忽略。 在 .gitignore 里面添加像 后端/*.env 对于更通用的你可以这样做 **/*.env

解决方案:

**/.env

解释:

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