一个文件已经添加到了版本库中,怎么从版本库中移除,变成忽略文件呢?
一 先找到 .gitignore文件,把文件或目录路径写入.gitignore
假设这个是配置文件 database.php
/.idea
/.vscode
/vendor
*.log
.env
config/database.php
假如database.php文件从未加入过版本库,使用 git status命令会发现文件被忽略了。
git status
二 删除git版本库本地缓存
但在项目开发过程中,有时候想把某些目录或文件加入忽略规则,按照上述方法定义.gitignore后发现并未生效,原因是.gitignore只能忽略那些原来没有被track的文件,如果文件已经被纳入了版本管理中,则修改.gitignore是无效的。
这类情况解决方法就是先把本地缓存删除(改变成未track状态),然后再提交
在本地代码执行以下命令
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
git push
在线上服务器执行以下命令
git pull
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
git push
最后修改本地的 database.php文件,发现Untracked files区已经忽略了database.php
git status
完
参考 https://blog.csdn.net/voke_/article/details/80508500