133

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

Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37
Manoj
  • 2,000
  • 2
  • 16
  • 21

10 Answers10

329
  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
rball
  • 6,925
  • 7
  • 49
  • 77
Karavolt
  • 3,299
  • 1
  • 8
  • 11
  • 3
    Aha! The key is `git rm -r --cached .` that's what I was forgetting. – Kyle Krzeski Aug 01 '19 at 19:50
  • 4
    Don't forget the `.` in `git rm -r --cached .` if you don't write the dot it wont work. It could be hard to see. – Hasan Sefa Ozalp Feb 10 '20 at 21:41
  • If you're using VSCode, you may need to restart it to have it "visually" update (i.e. this answer worked perfectly, but VSCode may not immediately grey out the node_modules folder). – Corey P Mar 10 '23 at 17:58
56

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!

Sheena Singla
  • 706
  • 7
  • 13
  • 2
    @TarunNagpal I assume you understand what commits and pushes are. The first command recursively (`-r`) removes (`rm`) the `--cached` `node_modules` directory. `rm -r` is a *nix command borrowed here by git. – Daniel Jul 29 '22 at 15:30
23

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)
Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37
15

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
Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
7

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

Shuwn Yuan Tee
  • 5,578
  • 6
  • 28
  • 42
6

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.

aderchox
  • 3,163
  • 2
  • 28
  • 37
4

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.
Paschal
  • 725
  • 1
  • 9
  • 17
4
  1. Create a file .gitignore

  2. Add node_modules/* line to gitignore file

  3. 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
    
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
3

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

Meryan
  • 1,285
  • 12
  • 25
-4

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.

Hendrik Jan
  • 4,396
  • 8
  • 39
  • 75