Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring node_modules using .gitignore

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?

like image 948
Carlo Avatar asked Jan 13 '18 14:01

Carlo


2 Answers

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

like image 73
Carlo Avatar answered Nov 15 '22 02:11

Carlo


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.

like image 41
Lajos Gallay Avatar answered Nov 15 '22 02:11

Lajos Gallay