[英]Git - Ignore node_modules folder everywhere
I have a project containing multiple other projects:我有一个包含多个其他项目的项目:
All containing node_modules
folder.所有包含node_modules
文件夹。 I want git to ignore the folder no matter where it is starting from the root folder.我希望 git 忽略该文件夹,无论它从根文件夹开始的位置。 Something like this to add in.gitignore:像这样添加 in.gitignore 的东西:
*node_modules/*
Use the universal one-liner in terminal in the project directory:在项目目录的终端中使用通用单行:
touch .gitignore && echo "node_modules/" >> .gitignore && git rm -r --cached node_modules ; touch .gitignore && echo "node_modules/" >> .gitignore && git rm -r --cached node_modules ; git status状态
It works no matter if you've created a .gitignore
or not, no matter if you've added node_modules
to git tracking or not.无论您是否创建了.gitignore
,无论您是否已将node_modules
添加到 git 跟踪,它都有效。
Then commit and push the .gitignore
changes.然后提交并推送.gitignore
更改。
Explanation解释
touch
will generate the .gitignore
file if it doesn't already exist.如果.gitignore
文件尚不存在, touch
将生成它。
echo
and >>
will append node_modules/
at the end of .gitignore
, causing the node_modules
folder and all subfolders to be ignored. echo
和>>
将在.gitignore
末尾附加node_modules/
,导致node_modules
文件夹和所有子文件夹被忽略。
git rm -r --cached
removes the node_modules
folder from git control if it was added before. git rm -r --cached
如果之前添加了node_modules
文件夹,则从 git 控件中删除它。 Otherwise, this will show a warning pathspec 'node_modules' did not match any files
, which has no side effects and you can safely ignore.否则,这将显示警告pathspec 'node_modules' did not match any files
,它没有副作用,您可以放心地忽略。 The flags cause the removal to be recursive and include the cache.这些标志导致删除是递归的并包括缓存。
git status
displays the new changes. git status
显示新的更改。 A change to .gitignore
will appear, while node_modules
will not appear as it is no longer being tracked by git.将出现对.gitignore
的更改,而node_modules
将不会出现,因为它不再被 git 跟踪。
In a new monorepo setup I found just using this在一个新的 monorepo 设置中,我发现只是使用这个
node_modules
solved it to ignore all the node_modules in the subdirectory, note there is no slash before or after which means recursive.解决它忽略子目录中的所有node_modules,注意之前或之后没有斜杠,这意味着递归。
**/node_modules
**
is used for a recursive call in the whole project **
用于整个项目中的递归调用
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 directoryfoo
anywhere, the same as patternfoo
.例如,**/foo
匹配任何地方的文件或目录foo
,与模式foo
相同。**/foo/bar
matches file or directorybar
anywhere that is directly under directoryfoo
.**/foo/bar
匹配文件或目录bar
直接位于目录foo
下的任何位置。A trailing
/**
matches everything inside.尾随/**
匹配里面的所有内容。 For example,abc/**
matches all files inside directoryabc
, relative to the location of the .gitignore file, with infinite depth.例如,abc/**
匹配目录abc
中的所有文件,相对于 .gitignore 文件的位置,具有无限深度。A slash followed by two consecutive asterisks then a slash matches zero or more directories.斜杠后跟两个连续的星号,然后斜杠匹配零个或多个目录。 For example,
a/\**/b
matchesa/b
,a/x/b
,a/x/y/b
and so on.例如,a/\**/b
匹配a/b
、a/x/b
、a/x/y/b
等。Other consecutive asterisks are considered invalid.其他连续的星号被认为是无效的。
node_modules/
为什么这种方法比node_modules/
更好The **
acts as a recursive pattern. **
充当递归模式。 It is useful in monorepo projects where you have node_modules in sub directories.它在子目录中有 node_modules 的 monorepo 项目中很有用。 **
will search for all the node_modules inside the directory & ignore them. **
将搜索目录中的所有 node_modules 并忽略它们。
First and foremost thing is to add .gitignore
file in my-app.首先也是最重要的事情是在 my-app 中添加.gitignore
文件。 Like so in image below.如下图所示。
and next add this in your .gitignore
file然后将其添加到您的.gitignore
文件中
/node_modules
You can also add others files too to ignore them to be pushed on github.您也可以添加其他文件以忽略它们以推送到 github。 Here are some more files kept in .gitignore.这里还有一些保存在 .gitignore 中的文件。 You can add them according to your requirement.您可以根据需要添加它们。 #
is just a way to comment in .gitignore file. #
只是在 .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*
Create .gitignore file in root folder directly by code editor or by command通过代码编辑器或命令直接在根文件夹中创建.gitignore文件
For Mac & Linux适用于 Mac 和 Linux
touch .gitignore
For Windows对于 Windows
echo >.gitignore
open .gitignore declare folder or file name like this /foldername像这样打开.gitignore声明文件夹或文件名/foldername
**node_modules
This works for me这对我有用
recursive approach to ignore all node_modules present in sub folders忽略子文件夹中存在的所有 node_modules 的递归方法
it will automatically create a .gitignore
file if not then create a file name .gitignore
and add copy & paste the below code如果没有,它将自动创建一个.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*
these below are all unnecessary files以下这些都是不必要的文件
See https://help.github.com/articles/ignoring-files/ for more about ignoring files.有关忽略文件的更多信息,请参阅https://help.github.com/articles/ignoring-files/ 。
and save the .gitignore
file and you can upload并保存.gitignore
文件,您可以上传
Add below line to your .gitignore将以下行添加到您的 .gitignore
/node_modules/
In my case, writing /node_modules without the foreslash after was not working就我而言,在没有前斜杠的情况下编写 /node_modules 是行不通的
you can do it with SVN/Tortoise git as well.你也可以用 SVN/Tortoise git 来做。
just right click on node_modules -> Tortoise git -> add to ignore list.只需右键单击 node_modules -> Tortoise git -> 添加到忽略列表。
This will generate .gitIgnore for you and you won't find node_modules folder in staging again.这将为您生成 .gitIgnore 并且您将不会在暂存中再次找到 node_modules 文件夹。
If your subproject/client node_modules gets committed,如果您的子项目/客户端 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*
then add "node_modules" at the last line.然后在最后一行添加“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 ------
Follow these steps -按着这些次序 -
CTRL + `
node_modules
**node_modules
Note : if you make spelling mistake while typing the name of the folder or file, it won't work.注意:如果您在输入文件夹或文件的名称时出现拼写错误,它将无法正常工作。 so, double check spelling所以,仔细检查拼写
将 node_modules/ 或 node_modules 添加到 .gitignore 文件以忽略当前文件夹和任何子文件夹中名为 node_modules 的所有目录。
In Mac,在 Mac 中,
If you wanna do it via the command line, type in echo node_modules/ >.gitignore
.如果您想通过命令行执行此操作,请输入echo node_modules/ >.gitignore
。 You can check if git is tracking the folder by typing in git status
.您可以通过输入git status
来检查 git 是否正在跟踪文件夹。 If its being tracked type in git rm -r --cached node_modules
.如果它在git rm -r --cached node_modules
中被跟踪类型。
Add below line to your .gitignore将以下行添加到您的 .gitignore
*/node_modules/*
This will ignore all node_modules in your current directory as well as subdirectory.这将忽略当前目录和子目录中的所有 node_modules。
只需将不同的 .gitignore 文件添加到迷你项目 1 和迷你项目 2。每个 .gitignore 文件都应该 /node_modules 并且你很高兴。
foe the ones who are trying the answers above and still facing the problem与那些尝试上述答案但仍面临问题的人为敌
I've tried almost all of the answers eventually , it fixed my problem with ignoring node_modules but only after i committed the changes !我最终尝试了几乎所有的答案,它解决了我忽略 node_modules 的问题,但只有在我提交更改之后!
It's not really a good practice to keep no modules in the git repository.在 git 存储库中不保留任何模块并不是一个好习惯。 # gitignore file To Remove node_modules Folder Create gitignore Remove node_modules # gitignore 文件 删除 node_modules 文件夹创建 gitignore 删除 node_modules
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.