我有一个项目包含多个其他项目:

主要项目 小项目1 小项目2

所有包含node_modules文件夹。我想要git忽略文件夹,无论它是从根文件夹开始的位置。在.gitignore中添加如下内容:

*node_modules/*

当前回答

如果你想通过命令行来做,输入echo node_modules/ > .gitignore。你可以通过输入git status来检查git是否在跟踪这个文件夹。如果被跟踪,在git中输入rm -r——cached node_modules。

其他回答

首先也是最重要的事情是在我的应用程序中添加。gitignore文件。如下图所示。

然后把这个添加到你的。gitignore文件中

/node_modules

Note

你也可以添加其他文件,忽略他们被推到github上。以下是一些保存在。gitignore中的文件。你可以根据你的要求添加。#只是在.gitignore文件中注释的一种方式。

# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

编辑-(2022-04-09之前)

在一个新的monorepo设置中,我发现只用这个

node_modules

解决了忽略子目录中所有node_modules的问题,注意前面和后面没有斜杠,这意味着递归。

参考

老路-(2022-04-09之前)

**/node_modules

**用于整个项目中的递归调用

Two consecutive asterisks ** in patterns matched against full pathname may have special meaning: 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. A trailing /** matches everything inside. For example, abc/** matches all files inside directory abc, relative to the location of the .gitignore file, with infinite depth. A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, a/\**/b matches a/b, a/x/b, a/x/y/b and so on. Other consecutive asterisks are considered invalid.

为什么这种方法比node_modules/更好

**充当递归模式。它在monorepo项目中很有用,因为你在子目录中有node_modules。**将搜索目录中所有的node_modules,并忽略它们。

参考

将node_modules/或node_modules添加到.gitignore文件中,以忽略当前文件夹中所有名为node_modules的目录和任何子文件夹。

只需在小项目1和小项目2中添加不同的.gitignore文件。每个.gitignore文件都应该/node_modules,这样就可以了。

你也可以用SVN/Tortoise git来做。

只需右击node_modules ->乌龟git ->添加到忽略列表。

这将为你生成. gitignore,你不会再在staging中找到node_modules文件夹。