git上传 项目 把node_modules也上传至仓库了,在文件.gitignore 中忽略node_modules 依然不行

前言

新建了一个vitepress 项目 但上传至github的时候不小心把node_modules 上传到仓库中了,于是我重新添加了 .gitignore 然后重新上传项目, 上次成功后却发现 node_modules 在仓库中依然存在

思考

这种情况可能是因为 Git 会继续跟踪已经被提交的文件,即使你在 .gitignore 中添加了 node_modules

解决

1.删除已经被 Git 跟踪的 node_modules 目录:

git rm -r --cached node_modules

2.确保 .gitignore 文件已正确配置:

确保 .gitignore 中已经正确包含了 node_modules:

node_modules

3.重新提交:

git add .
git commit -m "Remove node_modules from version control"
git push

这几步完成后,node_modules 就不会再出现在仓库中了。如果你再做一些修改并推送,Git 应该会忽略 node_modules 文件夹了。