Git - 忽略node_modules文件夹无处不在
本文翻译自:Git - Ignore node_modules folder everywhere
I have a project containing multiple other projects : 我有一个包含多个其他项目的项目:
- Main project 主要项目
- Mini project 1 迷你项目1
- Mini project 2 迷你项目2
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 : 像这样添加.gitignore:
*node_modules/*
#1楼
参考:https://stackoom.com/question/217Kv/Git-忽略node-modules文件夹无处不在
#2楼
Add this 加上这个
node_modules/
to .gitignore
file to ignore all directories called node_modules
in current folder and any subfolders .gitignore
文件忽略当前文件夹和任何子文件夹中名为node_modules
所有目录
#3楼
I got into this situation a few times, so I made a one-liner I can paste into terminal in my project directory: 我进入这种情况几次,所以我做了一个单行程,我可以粘贴到我的项目目录中的终端:
touch .gitignore && echo "node_modules/" >> .gitignore
Or, when I've added the node_modules
folder to git already: 或者,当我已经将node_modules
文件夹添加到git时:
git rm -r --cached node_modules && touch .gitignore && echo "node_modules/" >> .gitignore
Then, validate that it worked: 然后,验证它是否有效:
git status
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
和>>
将在.gitignore
的末尾追加node_modules/
,导致node_modules
文件夹和所有子文件夹被忽略。
git rm -r --cached
removes the node_modules
path from git control. git rm -r --cached
从git控件中删除node_modules
路径。 The flags cause the removal to be recursive and include the cache. 标志导致删除是递归的并包括缓存。
#4楼
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
Note 注意
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*
#5楼
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 directory "foo" anywhere, the same as pattern "foo". 例如,“ / foo”在任何地方匹配文件或目录“foo”,与模式“foo”相同。 "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo". “** / foo / bar”将文件或目录“bar”与直接位于“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. 例如,“abc / ”匹配目录“abc”内的所有文件,相对于.gitignore文件的位置,具有无限深度。
A slash followed by two consecutive asterisks then a slash matches zero or more directories. 斜杠后跟两个连续的星号,然后斜杠匹配零个或多个目录。 For example, "a/**/b" matches "a/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. 其他连续星号被视为无效。
#6楼
Create .gitignore file in root folder directly by code editor or by command 通过代码编辑器或命令直接在根文件夹中创建.gitignore文件
touch .gitignore
open .gitignore declare folder or file name like this /foldername 打开.gitignore声明文件夹或文件名,如/ foldername
转载:https://blog.csdn.net/xfxf996/article/details/108022822