本文翻譯自: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