.gitignore常见问题

git作为一个工具,用的熟练,确实可以大大提高工作效率,但如果不熟,就会遇到各种奇奇怪怪的问题

上周公司的一个项目,由于.gitignore配置问题,每次commit,都会提示系统文件需要git add,即使往.gitignore里进行添加,也不起作用。

/*.gitigonre文件 */

env/ *.pyc node_modules/ /logs/ /staticfiles/ /.env /.prd /.qa /caches/

这是因为:

.gitignore对已经纳入版本号库的文件不能生效。那对于新增的系统文件,如何让.gitignore识别来实现我们想要的结果呢?

查了一下官方文档:

if a file is already being tracked by Git, adding the file to .gitignore won’t stop Git from tracking it. You’ll need to do git rm --cached <file> to keep the file in your tree and then ignore it.

简单翻译一下就是:如果一个文件已经被git追踪,那么这个文件放入.gitignore后,git依然保持追踪状态。

知道了原理之后,就知道怎么解决了——将其从git版本控制里移除

git rm -r --cached <file>

如果有很多系统文件需要从git版本控制里移除,可以这样:

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

当然啦,最好还是养成在项目开始就创建.gitignore文件的习惯,这样就不会遇到push时,各种系统文件更新提示了。

原文地址:https://www.cnblogs.com/guojunru/p/6089254.html