.gitignore is not ignoring directories

Since the node_modules directory is already tracked as part of the repository, the .gitignore rule will not apply to it.

You need to untrack the directory from git using

git rm -r --cached node_modules
git commit -m "removing node_modules"

You can run the above 2 in git-bash.

After this, the .gitignore rule will ignore the directory away.

Note that this will remove the directory node_modules from your other repos once you pull the changes in. Only the original repo where you made that commit will still have the node_modules folder there.


Similar to Zach, I also used echo "node_modules/" >> .gitignore.

The problem was it had created the file with encoding UCS-2 LE BOM. Using notepad++ I changed the encoding to UTF-8 and voila - node_modules is now ignored.

enter image description here


If the files are already tracked the .gitignore file will not override this. You will need to use git rm --cached <files>

See the full details on git rm at https://www.kernel.org/pub/software/scm/git/docs/git-rm.html I ran into this once or twice myself early on with git and it was not quite what I expected either.


If you work with node projects, I like this .gitignore:

# See http://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
node_modules

# testing
coverage

# production
build

# misc
.DS_Store
.env
npm-debug.log

I had this problem. Somehow when I generated the file (using echo "node_modules" > .gitignore) it had inserted a garbage character at the beginning of the file (I blame powershell).

So, if you run into this problem try deleting your .gitignore and starting over.