Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gitignore not ignoring node_modules

Tags:

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/ 
like image 761
Rimil Dey Avatar asked Feb 05 '17 06:02

Rimil Dey


People also ask

Why is my Gitignore not ignoring?

gitignore list does not work to ignore files that are already committed into the Git repository. This means that if you: Make a commit to your Git repository, and then; Set up your .

Do I put node_modules in Gitignore?

Gotcha #2 - node_modules already committedIf you've already committed node_modules , you'll need to first remove the directory from git index. Otherwise, adding it to . gitignore won't make any difference.

Why is my .gitignore file not working?

Some times, even if you haven't added some files to the repository, git seems to monitor them even after you add them to the . gitignore file. This is a caching issue that can occur and to fix it, you need to clear your cache.

Does Gitignore apply to subdirectories?

There are several locations where Git looks for ignore files. Besides looking in the root folder of a Git project, Git also checks if there is a . gitignore in every subdirectory. This way you can ignore files on a finer grained level if different folders need different rules.


2 Answers

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.

like image 152
janos Avatar answered Oct 25 '22 19:10

janos


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.

like image 38
gbarrett1988 Avatar answered Oct 25 '22 20:10

gbarrett1988