Getting error while removing node_modules
in git, vscode terminal
git rm -r --cached node_modules
Error:
fatal error: pathspec 'node_modules' did not match any files
Getting error while removing node_modules
in git, vscode terminal
git rm -r --cached node_modules
Error:
fatal error: pathspec 'node_modules' did not match any files
git rm -r --cached .
git add .
git commit -m "remove gitignore files"
git push
I faced the same issue. Do the below steps -
Run below commands in your terminal
git rm -r --cached node_modules
git commit -am "node_modules be gone!"
git push origin master
That's all!
You are good to go!
Make .gitignore
file in your project directory.
.gitignore
file will look like this
/Folder_name
Type in below commands in the terminal or equivalent:
git rm -r --cached Folder_name (remove the directory from git but not delete it from the filesystem/locally)
git commit -m "remove unused directory"
git push origin <your-git-branch> (can be master or develop or others)
Create .gitignore
file and add node_modules
there:
touch .gitignore && echo "node_modules" >> .gitignore
Remove cached data:
git rm -r --cached .
Update your last commit:
git add .
git commit --amend --no-edit
git push --force-with-lease
The error means node_modules
directory is not under git version control. There are 2 possibilities:
They are not added to Git
yet. Try running git status
command, to see whether they appear as untracked
files.
It has already been added into your gitignore
file. To list all ignored files, try this command: git ls-files --others -i --exclude-standard
Note that the currently accepted answer does NOT delete it completely and it will remain in the history. If you want it to be deleted completely from your history, i.e., from all past commits in which this directory exists in, you can use either of these ways:
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch node_modules" HEAD
(Read more about it here: 7.6 Git Tools - Rewriting History)
Or, you can use a third-party plugin called filter repo - which is indeed the officially recommended method by Git too:
git filter-repo --path node_modules --invert-paths
(Read more on how to use here: Installation and Cheat Sheet and Documentation)
In both cases however, notice that you're re-writing the history which might be a nightmare when working in a team... . So either use this in your own local branch/environment, or make sure you know no one has any better options and let all of your mates know about it beforehand.
The above answers are great. They are just not things you remember all the time. So you have to check back online every time you need to do this.
Here is a quick way to remove node_modules with git commands you are already familiar with.
.gitignore
file and add /node_modules
to it.node_modules
folder and commit your changes.Git will interpret this as a folder deletion and will remove the node_modules folder on git push
from Github or whatever source control you use.
npm i
or yarn install
to recreate node_module from
package.json automatically in your development environment.Create a file .gitignore
Add node_modules/*
line to gitignore
file
Run the below commands in Your terminal
git rm -r --cached node_modules
git add .
git commit -m "Remove node_modules from git in vscode"
git push
Once git starts tracking a file or a folder it won't stop even if you add it to .gitignore
You will need to delete it from the .git repository. I exclusively use a local one, typically located at the root of the project's source.
As of today 20200408, it does not appear there is a mouse click approach to remove the annoying file/folder from git.
You will have to use the terminal.
Press Ctrl-J then select Terminal tab
or from VSCode menu select Terminal / New Terminal
Then issue something like the following command
git rm -r --cached www/index.html
The following exclude
www/
was already in my .gitignore file.
In your case you would want to add the "cancer folder" :)
node_modules/
PS: Simple things made unnecessarily hard, I wonder why?
Below is the zillion garbage of Git commands I never use. Enjoy!
You can remove folders from git (using the command-line or your IDE).
If you want to remove node_modules
from git:
node_modules
to node_modulesx
node_modulesx
back to node_modules
node_modules
to .gitignore (create the .gitignore file if needed)Now your node_modules
folder still exists, but will not be committed to git anymore.