如果先使用 npm
创建了项目,然后才创建的.gitignore文件,那么在提交时本地Git并不会忽略相应的文件和路径。
解决办法:
- 初始化 git init
- 关联远程仓库:git remote add origin XXX
- 然后提交代码
- 把本地的 node_modules 包删除
- 再次提交删除 node_modules 包后的代码
- 清理本地缓存(如果有):git rm -r --cached .
- 重新npm install下载 node_modules
- 拉取一次远程代码: git pull
- 重新提交本地修改的代码
- 发现 gitignore 生效了,新下载的node_modules依赖不再出现在未提交文件列表里