【发布时间】:2015-07-01 11:34:02
【问题描述】:
我有一个包含多个其他项目的项目:
- 主要项目
- 小项目1
- 小项目2
所有包含node_modules 的文件夹。我希望 git 忽略该文件夹,无论它从根文件夹开始的位置。在 .gitignore 中添加这样的东西:
*node_modules/*
【问题讨论】:
标签: git node-modules
我有一个包含多个其他项目的项目:
所有包含node_modules 的文件夹。我希望 git 忽略该文件夹,无论它从根文件夹开始的位置。在 .gitignore 中添加这样的东西:
*node_modules/*
【问题讨论】:
标签: git node-modules
【讨论】:
在项目目录的终端中使用universal one-liner:
touch .gitignore && echo "node_modules/" >> .gitignore && git rm -r --cached node_modules ;混帐状态
无论您是否创建了 .gitignore,无论您是否将 node_modules 添加到 git 跟踪,它都有效。
然后提交并推送.gitignore 更改。
说明
touch 将生成 .gitignore 文件(如果它尚不存在)。
echo 和>> 将在node_modules/ 末尾附加node_modules/,导致node_modules 文件夹和所有子文件夹被忽略。
git rm -r --cached 会从 git 控件中删除 node_modules 文件夹(如果之前添加了该文件夹)。否则,这将显示警告pathspec 'node_modules' did not match any files,它没有副作用,您可以放心地忽略。这些标志会导致递归删除并包括缓存。
git status 显示新的更改。将出现对 .gitignore 的更改,而 node_modules 将不会出现,因为它不再被 git 跟踪。
【讨论】:
touch 在这里不应该是必需的:>> 将创建该文件,如果它不存在。
touch 是必需的。 >> 将返回 no such file or directory: .gitignore 否则。
echo "node_modules/" > .gitignore 应该使用>> 在现有指定文件的末尾追加 文本创建一个具有原始内容的新 文件
**/node_modules
**用于整个项目的递归调用
与完整路径名匹配的模式中的两个连续星号
**可能具有特殊含义:前导
**后跟斜杠表示在所有目录中匹配。例如,**/foo匹配任意位置的文件或目录foo,与模式foo相同。**/foo/bar匹配文件或目录bar直接位于目录foo下的任何位置。结尾的
/**匹配里面的所有内容。例如,abc/**匹配目录abc内的所有文件,相对于 .gitignore 文件的位置,具有无限深度。斜杠后跟两个连续的星号,然后斜杠匹配零个或多个目录。比如
a/\**/b匹配a/b、a/x/b、a/x/y/b等等。其他连续的星号被认为是无效的。
node_modules/
** 充当递归模式。它在子目录中有 node_modules 的 monorepo 项目中很有用。 ** 将搜索目录中的所有 node_modules 并忽略它们。
【讨论】:
node_modules/更好?
node_modules 文件夹,与它们所在的位置无关(例如/BackOffice/frontend/node_modules)
**,没有它也一样
首先最重要的事情是在我的应用程序中添加.gitignore 文件。如下图所示。
然后将其添加到您的 .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*
【讨论】:
直接通过代码编辑器或命令在根文件夹创建.gitignore文件
适用于 Mac 和 Linux
触摸.gitignore
对于 Windows
回声 >.gitignore
打开 .gitignore 像这样声明文件夹或文件名 /foldername
【讨论】:
echo >.gitignore。 ;)
**node_modules
这对我有用
忽略子文件夹中存在的所有 node_modules 的递归方法
【讨论】:
它会自动创建一个.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文件即可上传
【讨论】:
你也可以用 SVN/Tortoise git 来做。
只需右键单击 node_modules -> Tortoise git -> 添加到忽略列表。
这将为你生成 .gitIgnore 并且你不会在 staging 中再次找到 node_modules 文件夹。
【讨论】:
如果您的子项目/客户端 node_modules 被提交,
# 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*
然后在最后一行添加“node_modules”。
# 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*
node_modules
# ------ Up Here ------
【讨论】:
按照这些步骤-
CTRL + `
node_modules
**node_modules
注意:如果您在输入文件夹或文件的名称时出现拼写错误,将无法使用。所以,仔细检查拼写
【讨论】:
将 node_modules/ 或 node_modules 添加到 .gitignore 文件以忽略当前文件夹和任何子文件夹中所有名为 node_modules 的目录。
【讨论】:
将下面的行添加到您的 .gitignore
*/node_modules/*
这将忽略当前目录和子目录中的所有 node_modules。
【讨论】:
只需将不同的 .gitignore 文件添加到迷你项目 1 和迷你项目 2 中。每个 .gitignore 文件都应该 /node_modules 并且你很高兴。
【讨论】:
与那些尝试上述答案但仍然面临问题的人为敌
我最终尝试了几乎所有的答案,它解决了我忽略 node_modules 的问题,但只有在我提交更改之后!
【讨论】: