.gitignore not working even when I clear the cache

Viewed 2249

I made the mistake of creating a gitignore file after I had already pushed an initial commit to my git repo.

My .gitignore is very simple, it only contains:

node_modules

I've tried the following:

git rm --cached -rf .
git add .
git commit 'Fix untracked files'
git push

I've also tried deleting my .git folder in my project directory and reinitializing the entire project.

Both times it still pushes my node_modules directory!

How do I fix this?

1 Answers
  1. First, remove the node_modules from .gitignore file (so, .gitignore is empty now).

  2. Clean the git cache and Delete the node_modules folder.

    $ git rm -r --cached node_modules    # clean git cache
    $ rm -rf node_modules                # delete node_modules
    
  3. Do git Add, Commit, Push to remote. Now, node_modules will be removed from remote.

    $ git add -A
    $ git commit -m 'Delete node_modules'
    $ git push origin HEAD
    
  4. Add node_modules in .gitignore file, clear git cache, generate node_modules folder.

    # add 'node_modules/' into .gitignore
    
    $ git rm -r --cached node_modules
    $ npm i          # generate node_modules
    
    $ git status     # see if git ignores node_modules folder now
    
  5. Do git Add, Commit, Push to remote. Now .gitignore contains node_modules so git will ignore the folder in future.

    $ git add .
    $ git commit -m 'Add node_modules in .gitignore'
    $ git push origin HEAD 
    
Related