我使用npm开始了一个项目,添加了一些依赖项,然后使用git init
初始化了仓库。
我希望目录node_modules
被git忽略,所以我将它添加到.gitignore
文件中,如下所示。
.gitignore
node_modules/
当然,因为
git init
之前添加了node_modules
,所以它仍然被识别为要跟踪的目录。因此,我删除了它,并使用以下命令(根据类似问题的建议):
git rm -r --cached .
git add .
git commit -m ".gitignore should now work"
然后使用npm install
重新安装npm依赖项。
完成后,我希望目录node_modules
最终被忽略。
但是,如果我输入git status
,会得到以下结果:
Untracked files:
(use "git add <file>..." to include in what will be committed)
node_modules/
为什么?
git init; echo "Myfile" > myfile; mkdir node_modules && echo "module" > node_modules/module.txt; git commit -m "init"; echo "node_modules" > .gitignore; rm -rf node_modules/; git rm -r --cached .; git add .; git commit -m ".gitignore should now work"; mkdir node_modules; echo "module" > node_modules/mod.txt;
在运行这些命令后,git status
显示 "On branch master nothing to commit, working tree clean"。 - Blessgit status
后仍然把node_modules/
当作未跟踪。 - Carlogit rm -r --cached .
是解决方案。运行此命令后,.gitignore
中的node_modules
就可以正常工作了。 - Felix Htoo