Ignoring node_modules using .gitignore

蓝咒 提交于 2020-08-24 11:15:27

问题


I started a project using npm, added a few dependencies and then initialized the repository usign git init.

I wanted the directory node_modules to be ignored by git, so I added it to the .gitignore file like so.

.gitignore

node_modules/

Of course because node_modules was added before git init, it is still recognized as a directory to track.

So I deleted it, used the following commands (as suggesed for similar problems)

git rm -r --cached .
git add .
git commit -m ".gitignore should now work"

Then reinstalled the npm dependencies by using npm install

After this I expect to have the directory node_modules to be finally ignored. Instead if I type git status I get

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    node_modules/

Why?


回答1:


Under this question there are many possible fixes for a .gitignore file not properly working.

In this case the problem is (was) that the .gitignore file's encoding was UNICODE instead of ASCII




回答2:


Try the following:

  • Remove node_modules from .gitignore and save it

  • Delete node_modules (or move it somewhere outside from the project directory)

  • Commit the changes (there will be a tons of deletion from node_modules) This step will remove the files from source control.

  • Add node_modules to .gitignore again

  • Commit gitignore

  • Re-run npm install or restore the node_modules directory.



来源:https://stackoverflow.com/questions/48240882/ignoring-node-modules-using-gitignore

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