Remove node_modules from git in vscode

GitNode Modules

Git Problem Overview


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 Solutions


Solution 1 - Git

  1. make .gitignore file.
  2. add node_modules/ line to gitignore file
  3. run this command git rm -r --cached . git add . git commit -m "remove gitignore files" git push

Solution 2 - Git

I faced the same issue. Do the below steps -

  • Make .gitignore file.
  • 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!

Solution 3 - Git

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)

Solution 4 - Git

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

Solution 5 - Git

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

Solution 6 - Git

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.

Solution 7 - Git

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!

enter image description here

Solution 8 - Git

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.

  • First create a .gitignore file and add /node_modules to it.
  • Then delete the 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.

  • Then run npm i or yarn install to recreate node_module from package.json automatically in your development environment.

Solution 9 - Git

You can remove folders from git (using the command-line or your IDE).
If you want to remove node_modules from git:

  1. rename node_modules to node_modulesx
  2. commit
  3. rename node_modulesx back to node_modules
  4. add node_modules to .gitignore (create the .gitignore file if needed)
  5. commit

Now your node_modules folder still exists, but will not be committed to git anymore.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManoView Question on Stackoverflow
Solution 1 - GitKaravoltView Answer on Stackoverflow
Solution 2 - GitSheena SinglaView Answer on Stackoverflow
Solution 3 - GitAnkit JindalView Answer on Stackoverflow
Solution 4 - GitDmitry GrinkoView Answer on Stackoverflow
Solution 5 - GitShuwn Yuan TeeView Answer on Stackoverflow
Solution 6 - GitaderchoxView Answer on Stackoverflow
Solution 7 - GitMeryanView Answer on Stackoverflow
Solution 8 - GitPaschalView Answer on Stackoverflow
Solution 9 - GitHendrik JanView Answer on Stackoverflow