33

Even after adding the .gitignore file to the root of the .git repository, I cannot make git ignore the node_modules directory.

The files have not been added for Git to track.

I have gone through earlier stack overflow questions, and also tried adding a comment line to the first line, since apparently Git doesn't read the first line of that file, and it still doesn't work. I have tried to use the following command also, with no avail:

git rm --cached -r .

Could anyone help me out? The content of .gitignore:

#first line
node_modules/
janos
  • 120,954
  • 29
  • 226
  • 236
Rimil Dey
  • 827
  • 2
  • 13
  • 33
  • 1
    Did you put `.gitignore` inside `.git` or inside the directory containing the `.git` directory? – merlin2011 Feb 05 '17 at 07:03
  • inside the .git – Rimil Dey Feb 05 '17 at 07:03
  • I had this issue and it was because the changes were staged from a stash restore after temporarily switching to a branch that didn't have the gitignore...unstaging was the facepalm but correct solution. – WBT Oct 08 '22 at 19:37

12 Answers12

21

I don't know if this will help anyone but I created my .gitignore file in PS with echo "" > .gitignore and thus the encoding was set to UTF-16LE and not UTF-8. This caused problems with .gitignore working properly.

gbarrett1988
  • 221
  • 2
  • 3
  • 2
    Okay, but how do we change this? This solution simply explains a potential problem, but still offers no solution. It's like saying, you're car has a problem with the spark plugs in the engine block, and then walking out of the room immediately. – Jack_Hu Aug 08 '21 at 11:22
  • I agreed so hard I did the sleuthing. The second comment on this answer is what would do it. Having said that it's probably easiest to just use the gui and re-create the file via context menu https://stackoverflow.com/a/5596984/364632 – cBlaine Jan 15 '22 at 14:25
18

Even after adding the .gitignore file to the root of the .git repository, I cannot make git ignore the node_modules directory.

The .gitignore file should be placed in the root of the working tree (also known as the root of your project), which is just above the .git directory. Inside the .git directory the file has no effect.

This makes sense, because normally you want to put this file under version control, to share the list of ignored files with all developers on the project. And only the files inside the working tree, outside .git are under version control, the .git directory is for Git's internal storage.

(If you wanted to ignore patterns only locally, without adding to version control, you could do so by defining patterns in .git/info/exclude.)

[...] and also tried adding a comment line to the first line, since apparently Git doesn't read the first line of that file

For the record, it does read the first line too, there's nothing special about the first line in .gitignore.

janos
  • 120,954
  • 29
  • 226
  • 236
  • 5
    Answered like a boss. Was looking everywhere for info on how to structure `.gitignore` and where it should live. Everyone says, "just make a `.gitignore`", yet nobody says the exactitudes...and we know these computer fancy pants machines are wrought by exactness...;) +1 for comprehensive awesomeness. – twknab Feb 23 '17 at 00:17
  • 9
    If you already started versioning the node_modules, remember to `git rm --cached `, this will remove them from the cache and will start reading the new/updated `.gitignore` – OK999 Sep 14 '17 at 19:40
11

I solved the problem by deleting the file name at .gitignore and made it an invisible file respectively a dot-file, just like naming the file .gitignore and keeping the expression

node_modules/

in that file, so that works and now git ignores finally the node-modules directory. Don't know exactly, why this is working, but at least it does.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
11

If all fail you might want to try this. I was having the same issue and nothing worked.

Delete your node_modules folder. Push the changes to github (if you need to remove the node_modules folder from the repo) Make sur you have the node_modules/ line in the .gitignore file Run npm install. Push again.

I was working under Visual Studio Code and notice that you have to make the gitignore file before you run npm install. Not sure why but it fixed it in my case.

MadeInDreams
  • 1,991
  • 5
  • 33
  • 64
  • This was the one for me. What actually happened is that I accidentally committed the node_modules to my branch (oops!). I needed to delete the modules folder, update my ignore file to add include the modules folder, commit the deletion of the folder as well as the new gitignore, then run `npm i` once again and all was well – Arbiter Oct 20 '21 at 23:19
5

I deleted my node_modules folder and committed my changes. Then I copied the contents of my .gitignore file and deleted it. Recreated the file, pasted back in my clipboard contents and ran npm i . Fixed the issue. I am guessing that if you work on a VM or remote desktop and are using windows than bring the repo onto a mac it can cause encoding errors like mentioned above.

In Short:

copy .gitignore contents

delete .gitignore

re-create .gitignore

paste in clipboard.

Voila

Dharman
  • 30,962
  • 25
  • 85
  • 135
Curtis M
  • 905
  • 1
  • 8
  • 14
3

In my case, solved by converting file encoding from 'Unicode' to 'UTF-8 Without BOM' since I created .gitignore file via Powershell by redirecting output.

Initially I changed line ending from Windows (CRLF) to Linux (LF), but did not help.

guneysus
  • 6,203
  • 2
  • 45
  • 47
2

In my case i had it written with quotation marks like this

'functions/node_modules/'

instead of

functions/node_modules/
Stanislau Buzunko
  • 1,630
  • 1
  • 15
  • 27
1

My project structure was as follows:

project_name
 |
 |__ client
 |      |_ node_modules/
 |
 |__ server

$ sudo nano .gitignore 

Changed # dependencies 
        /node_modules to client/node_modules

$ git add .     /* to add modified gitignore file */
$ git status    /* to observe changes */
jaminyah
  • 11
  • 1
0

In my case I created two ".gitignore" file one in the main directory and second in the client folder

Wasim Al
  • 11
  • 2
0

Make sure you always have a new line at the end of your file. Might be helpful for you if you forgot one after your node_modules/ declaration.

poroia
  • 45
  • 1
  • 10
0

This is for whose who cames for a solution like me and non of the above worked

All I did is removing the .git/ and node_modules/ folder and then i redid the commands git init git add . ... etc and than i pushed my dir with no problem.

0

in my case it was ' node_modules/' instead of 'node_modules/' the space before node_modules was the problem