You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 20:45:23