Git - 忽略 node_modules 文件夹的所有位置

667

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

  • 主项目
    • 迷你项目1
    • 迷你项目2

所有项目都包含node_modules文件夹。我希望Git无论从根文件夹开始忽略该文件夹。类似这样在.gitignore中添加:

*node_modules/*
18个回答

1292

node_modules/ 或者 node_modules 添加到 .gitignore 文件中,来忽略当前文件夹和子文件夹中所有名为 node_modules 的目录,就像下面的图片一样。

example


对于Karma,排除项的全局路径是什么? - SuperUberDuper
16
针对 Node.js 的 .gitignore 模板:https://github.com/github/gitignore/blob/master/Node.gitignore - Xavier Egea
23
如果您之前将 node_modules 文件夹添加到 git 中,请查看 @AndrewSchreiber 的答案。您需要使用 "git rm -r --cached node_modules" 命令从缓存中删除它。 - Casper
启用后,保存了10000多个文件提交,这些提交之前一直让我无法使用git :D。 - m4n0
你不需要在结尾加上斜杠。 - Software Engineer
这个问题需要一个答案。在社区做出决定之前,请选择一个答案。 - Pogrindis

305

在项目目录中使用终端输入以下通用单行命令:

touch .gitignore && echo "node_modules/" >> .gitignore && git rm -r --cached node_modules ; git status

无论您是否创建了.gitignore文件,无论您是否已将node_modules添加到git跟踪或未添加,此命令都能正常工作。

然后提交并推送.gitignore更改。

说明:

touch会生成.gitignore文件(如果不存在)。

echo>>将在.gitignore末尾追加node_modules/,导致node_modules文件夹及其所有子文件夹被忽略。

git rm -r --cached从git控制中删除node_modules文件夹(如果之前添加过)。否则,这将显示警告:pathspec 'node_modules' did not match any files,这没有副作用,您可以安全地忽略。标志使删除递归,并包括缓存。

git status 显示新的更改。 .gitignore的更改将显示,而node_modules不会出现在跟踪列表中。


6
这里不需要使用touch:如果文件不存在,>>会创建该文件。 - lionello
1
需要使用 touch 命令。否则,执行 >> 会返回 no such file or directory: .gitignore - Andrew Schreiber
1
嗯,似乎因操作系统而异。 - lionello
2
echo "node_modules/" > .gitignore 应该创建一个 新的 文件,并使用 >> 将文本追加到指定文件的末尾。 - chrisz
@AndrewSchreiber 这个命令是应该在主项目文件夹还是子项目文件夹中应用? - Aravinda KS
显示剩余2条评论

124

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

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

node_modules

解决方法是忽略子目录中的所有 node_modules,注意前后没有斜杠,这意味着递归。

参考

旧方式 - (2022-04-09 之前)

**/node_modules

**在整个项目中用于递归调用。

对全路径名匹配的模式中,两个连续的星号 ** 可能具有特殊意义:

以斜杠开头的 ** 表示匹配所有目录。例如,**/foo 匹配任何位置的文件或目录 foo,与模式 foo 相同。 **/foo/bar 匹配直接位于目录 foo 下的文件或目录 bar

/** 结尾的表示匹配其中的所有内容。例如,abc/** 匹配相对于 .gitignore 文件所在位置的目录 abc 中的所有文件,无限深度。

斜杠后面跟着两个连续的星号和一个斜杠表示匹配零个或多个目录。例如,a/\**/b 匹配 a/ba/x/ba/x/y/b 等等。

其他连续的星号被视为无效。

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

**作为递归模式。在monorepo项目中非常有用,其中您在子目录中拥有node_modules。**将搜索目录中的所有node_modules并忽略它们。

参考


4
能否解释一下,为什么这种方法比简单使用node_modules/更好? - Advena
4
如果使用单一代码库(Monorepo),可能需要处理多个前端。这种方法会递归忽略所有 node_modules 文件夹,无论它们位于何处(例如 /BackOffice/frontend/node_modules)。 - Billybobbonnet
2
你不需要 **,没有它也能正常工作。 - Software Engineer
1
那对我没用,这就是我创建这个答案的原因。针对React Native Monorepo和Firebase与Cloud Functions。 - Rajendran Nadar
这很疯狂。'想去散步吗?''把它们消灭掉。那会解决问题。在你的“代码”上加上免责声明。 - Pogrindis

33

首要的事情是在my-app中添加.gitignore文件。就像下面图片中所示。

Put .gitignore in the parent folder/directory of node_modules.

接下来在你的.gitignore文件中添加这个。

/node_modules

注意

您也可以添加其他文件来忽略它们的上传到 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*

2
为什么要将构建文件添加到.gitignore中? - Sebastian Nielsen
11
@SebastianNielsen我们不跟踪node_modules的原因与此相同。编译会生成build/目录。 - Dan
1
请勿在开头添加斜杠,这会阻止递归。 - Software Engineer
@SoftwareEngineer 你能创建UR吗? - Mustkeem K
@MustkeemK - 抱歉,UR是什么? - Software Engineer

14
在.gitignore文件中添加以下行将忽略整个代码仓库中的node模块。
node_modules

在此输入图片描述


2
这是递归。 - Software Engineer

7

通过代码编辑器或命令直接在根目录下创建.gitignore文件

对于Mac和Linux

 touch .gitignore 

对于Windows

 echo >.gitignore 

打开.gitignore,声明文件夹或文件名称,例如/foldername


1
我猜你的意思是 echo >.gitignore。 ;) - lapo

6
**node_modules

这对我有效

递归方法忽略所有子文件夹中存在的node_modules


4
如果没有创建.gitignore文件,它将自动创建一个,否则创建一个名为.gitignore的文件并添加以下代码:
# dependencies
/node_modules
/.pnp
.pnp.js

# 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*

以下都是不必要的文件。
请参阅 https://help.github.com/articles/ignoring-files/ 了解有关如何忽略文件的更多信息。
保存 .gitignore 文件,然后您可以上传。

2

将以下行添加到您的.gitignore文件中:

/node_modules/

在我的情况下,如果没有斜杠,写成/node_modules是无效的。


2
如果您想通过命令行完成此操作,请输入echo node_modules/ > .gitignore。您可以通过输入git status来检查git是否跟踪该文件夹。如果正在跟踪,请输入git rm -r --cached node_modules

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接