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
Git - Ignore node_modules folder everywhere
提问by Mehdiway
I have a project containing multiple other projects :
我有一个包含多个其他项目的项目:
- Main project
- Mini project 1
- Mini project 2
- 主要项目
- 小项目1
- 小项目2
All containing node_modules
folder. 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 .gitignore
file to ignore all directories called node_modules
in current folder and any subfolders
to .gitignore
file 忽略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 .gitignore
or not, no matter if you've added node_modules
to git tracking or not.
无论您是否创建了git 跟踪.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.
touch
.gitignore
如果文件不存在,将生成该文件。
echo
and >>
will append node_modules/
at the end of .gitignore
, causing the node_modules
folder and all subfolders to be ignored.
echo
并且>>
将追加node_modules/
在结尾.gitignore
,造成node_modules
文件夹和子文件夹都将被忽略。
git rm -r --cached
removes the node_modules
folder 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 --cached
node_modules
如果之前添加过该文件夹,则从 git 控件中删除该文件夹。否则,这将显示一个警告pathspec 'node_modules' did not match any files
,它没有副作用,您可以放心地忽略。这些标志导致删除是递归的并包括缓存。
git status
displays the new changes. A change to .gitignore
will appear, while node_modules
will 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,**/foo
matches file or directoryfoo
anywhere, the same as patternfoo
.**/foo/bar
matches file or directorybar
anywhere that is directly under directoryfoo
.A trailing
/**
matches everything inside. For example,abc/**
matches all files inside directoryabc
, 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
matchesa/b
,a/x/b
,a/x/y/b
and so on.Other consecutive asterisks are considered invalid.
**
与完整路径名匹配的模式中的两个连续星号可能具有特殊含义:前导
**
后跟斜杠表示在所有目录中匹配。例如,**/foo
匹配foo
任意位置的文件或目录,与模式相同foo
。**/foo/bar
匹配bar
直接位于 directory 下的任何文件或目录foo
。尾随
/**
匹配里面的所有内容。例如,abc/**
匹配 directory 中的所有文件abc
,相对于 .gitignore 文件的位置,具有无限深度。斜杠后跟两个连续的星号,然后斜杠匹配零个或多个目录。例如,
a/\**/b
比赛a/b
,a/x/b
,a/x/y/b
等等。其他连续的星号被认为是无效的。
回答by Mustkeem K
First and foremost thing is to add .gitignore
file in my-app. Like so in image below.
首先也是最重要的事情是.gitignore
在我的应用程序中添加文件。如下图所示。
and next add this in your .gitignore
file
然后将其添加到您的.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
回答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。