8

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?

Carlo
  • 642
  • 1
  • 5
  • 16
  • Is this exactly the sequence you used? I cannot reproduce this result. – Dan Lowe Jan 13 '18 at 16:17
  • @DanLowe yes, I have double and triple checked, but nothing, it doesn't work.This looks absurd to me. – Carlo Jan 13 '18 at 16:39
  • I too couldn't reproduce this. Tried the following steps: `git init; echo "Myfile" > myfile; mkdir node_modules && echo "module" > node_modules/module.txt; git commit -m "init"; echo "node_modules" > .gitignore; rm -rf node_modules/; git rm -r --cached .; git add .; git commit -m ".gitignore should now work"; mkdir node_modules; echo "module" > node_modules/mod.txt; `. `git status` gives "On branch master nothing to commit, working tree clean" after running these. – Bless Jan 18 '18 at 15:52
  • @bless just tried again doing the same you did, but still have `node_modules/` as untracked after `git status` – Carlo Jan 18 '18 at 16:12
  • `git rm -r --cached .` was the solution for me. After running this command, `node_modules` from `.gitignore` worked well. – Felix Htoo Jun 02 '21 at 16:17

3 Answers3

9

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

Carlo
  • 642
  • 1
  • 5
  • 16
7

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.

Lajos Gallay
  • 1,169
  • 7
  • 16
  • 1
    Still not working, this is ridicolous, looks like a very stupid problem – Carlo Jan 17 '18 at 21:59
  • Worked for me. Be careful about .gitignore file path. – Taady Sep 15 '21 at 16:42
  • Neat! I cloned a repo and it had `node_modules` tracked but when I check the `.gitignore` it says its ignoring the `node_modules`. I think someone committed the `node_modules` and then added it to `.gitignore` in my case which isn't the ideal order of things for git. – KeshavDulal Jan 16 '23 at 06:30
-1

Don't forget to add .gitignore to git too

git add .gitignore

  • when you do git add . It automatically adds everthing. – Hritik Sharma Jun 17 '23 at 11:14
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34542742) – zerocukor287 Jun 19 '23 at 11:40