Git - 到处忽略 node_modules 文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29820791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:06:56  来源:igfitidea点击:

Git - Ignore node_modules folder everywhere

gitnode-modules

提问by Mehdiway

I have a project containing multiple other projects :

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

  • Main project
    • Mini project 1
    • Mini project 2
  • 主要项目
    • 小项目1
    • 小项目2

All containing node_modulesfolder. I want git to ignore the folder no matter where it is starting from the root folder. Something like this to add in .gitignore :

所有包含node_modules文件夹。我希望 git 忽略该文件夹,无论它从根文件夹开始。在 .gitignore 中添加这样的东西:

*node_modules/*

回答by Alik

Add this

添加这个

node_modules/

to .gitignorefile to ignore all directories called node_modulesin current folder and any subfolders

to .gitignorefile 忽略node_modules当前文件夹和任何子文件夹中调用的所有目录

回答by Andrew Schreiber

Use the universal one-linerin terminal in the project directory:

在项目目录中的终端中使用通用单行

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

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

It works no matter if you've created a .gitignoreor not, no matter if you've added node_modulesto git tracking or not.

无论您是否创建了git 跟踪.gitignore,无论您是否添加node_modules了 git 跟踪,它都可以工作。

Then commit and push the .gitignorechanges.

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

Explanation

解释

touchwill generate the .gitignorefile if it doesn't already exist.

touch.gitignore如果文件不存在,将生成该文件。

echoand >>will append node_modules/at the end of .gitignore, causing the node_modulesfolder and all subfolders to be ignored.

echo并且>>将追加node_modules/在结尾.gitignore,造成node_modules文件夹和子文件夹都将被忽略。

git rm -r --cachedremoves the node_modulesfolder from git control if it was added before. Otherwise, this will show a warning pathspec 'node_modules' did not match any files, which has no side effects and you can safely ignore. The flags cause the removal to be recursive and include the cache.

git rm -r --cachednode_modules如果之前添加过该文件夹,则从 git 控件中删除该文件夹。否则,这将显示一个警告pathspec 'node_modules' did not match any files,它没有副作用,您可以放心地忽略。这些标志导致删除是递归的并包括缓存。

git statusdisplays the new changes. A change to .gitignorewill appear, while node_moduleswill not appear as it is no longer being tracked by git.

git status显示新的更改。.gitignore将出现更改,而node_modules不会出现,因为它不再被 git 跟踪。

回答by Rajendran Nadar

Try doing something like this

尝试做这样的事情

**/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, **/foomatches file or directory fooanywhere, the same as pattern foo. **/foo/barmatches file or directory baranywhere 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/\**/bmatches a/b, a/x/b, a/x/y/band so on.

Other consecutive asterisks are considered invalid.

**与完整路径名匹配的模式中的两个连续星号可能具有特殊含义:

前导**后跟斜杠表示在所有目录中匹配。例如,**/foo匹配foo任意位置的文件或目录,与模式相同foo**/foo/bar匹配bar直接位于 directory 下的任何文件或目录foo

尾随/**匹配里面的所有内容。例如,abc/**匹配 directory 中的所有文件abc,相对于 .gitignore 文件的位置,具有无限深度。

斜杠后跟两个连续的星号,然后斜杠匹配零个或多个目录。例如,a/\**/b比赛a/ba/x/ba/x/y/b等等。

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

Reference

参考

回答by Mustkeem K

First and foremost thing is to add .gitignorefile in my-app. Like so in image below.

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

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

将 .gitignore 放在 node_modules 的父文件夹/目录中。

and next add this in your .gitignorefile

然后将其添加到您的.gitignore文件中

/node_modules

Note

笔记

You can also add others files too to ignore them to be pushed on github. Here are some more files kept in .gitignore. You can add them according to your requirement. #is just a way to comment in .gitignore file.

您也可以添加其他文件以忽略它们在 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*

回答by Amit Kumar

Create .gitignorefile in root folder directly by code editor or by command

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

For Mac & Linux

适用于 Mac 和 Linux

 touch .gitignore 

For Windows

对于 Windows

 echo .gitignore 

open .gitignoredeclare folder or file name like this /foldername

打开.gitignore像这样声明文件夹或文件名/foldername

回答by Umesh Khurana

Adding below line in .gitignore will ignore node modules from the entire repository.

在 .gitignore 中添加以下行将忽略整个存储库中的节点模块。

node_modules

enter image description here

在此处输入图片说明

回答by Dhruvesh Patel

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 文件夹。

回答by Hanzla Habib

**node_modules

This works for me

这对我有用

recursive approach to ignore all node_modules present in sub folders

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

回答by Manish Dubey

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。