Git忽略指定文件夹但保留其中单个文件的配置方法咨询
Git忽略规则配置:排除Features文件夹所有文件仅保留username.php
要实现排除Apps/Features下所有文件、只保留username.php的需求,你需要利用Git忽略规则的反向匹配特性,按以下方式配置.gitignore文件:
具体配置代码
# 先忽略Apps/Features目录下的所有内容 Apps/Features/* # 再单独取消对username.php的忽略 !Apps/Features/username.php
关键注意点
- 别直接写
Apps/Features/忽略整个文件夹——一旦文件夹被标记为忽略,里面的文件就算加!反向规则也不会生效,必须先忽略文件夹内的所有内容,再单独保留目标文件。 - 如果之前已经把
Apps/Features里的文件提交过仓库,得先清除Git缓存再重新提交:git rm -r --cached Apps/Features git add .gitignore git commit -m "更新忽略规则:仅保留Features下的username.php"
内容的提问来源于stack exchange,提问作者max
相关产品推荐
相关产品推荐

