在 vscode 中从 git 中删除 node_module

在 git、 vscode 终端中删除 node_modules时出错

 git rm -r --cached node_modules

错误:

致命错误: Pathspec‘ node _ module’不匹配任何文件

106494 次浏览
  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

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

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!

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

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)

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.

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 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.

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.

This solution was great for my problem in deploying for heroku, where i pushed the node_modules to heroku git

  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