Git - Ignore node_modules folder everywhere

我们两清 提交于 2019-11-27 05:51:28

Add this

node_modules/

to .gitignore file to ignore all directories called node_modules in current folder and any subfolders

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:

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.

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

git rm -r --cached removes the node_modules path from git control. The flags cause the removal to be recursive and include the cache.

Asif J

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

and next add this in your .gitignore file

/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.

# 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*

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/bar" matches file or directory "bar" anywhere 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/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.

Other consecutive asterisks are considered invalid.

Reference

Create .gitignore file in root folder directly by code editor or by command

 touch .gitignore 

open .gitignore declare folder or file name like this /foldername

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!