gitignore 例子 只包含特定文件

本文介绍了如何在`.gitignore`文件中配置规则,以排除某些目录并仅包含特定后缀的文件。例如,排除`dirignore1`和`dirignore2`目录,同时保留.c, .h, .cpp等文件。通过添加感叹号`!`,可以明确指定要包含的文件类型。需要注意的是,不能在排除父目录的情况下包含其子文件。参考了StackOverflow上的相关问题解答。" 103592098,8708872,Vue3项目中vw适配移动端详解,"['前端开发', 'Vue', '移动端适配', 'CSS', 'PostCSS']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

git工程只上传必须的文件,所以需要在一个文件(跟目录.gitignore)下面记录需要忽略的文件或文件夹。下面举例.gitignore, 该模版适合去除一些指定目录,并且仅仅包含其他目录下的特定结尾的文件:


*
!/**/
##去除哪些路径
/dirignore1/
/dirignore2/
##只包含哪些结尾的文件
!*.c
!*.h
!*.cpp
!*.sh
!*.xml
!*.py
!*.md
!*.jar
!.scala
!.java

1 * 代表忽略全部

2 /** 代表文件夹下的全部内容 ( A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc")

为什么要写这句,因为不可能包含一个文件,如果他的文件夹都被忽略了 It is not possible to re-include a file if a parent directory of that file is excluded. (*)

3 包含每个文件后,/dirignore1/, 在去除不需要的文件夹。(可以注意到.gitignore是按照顺序读入规则的,顺序重要)

4 ! 在每行前加感叹号,表示需要包括后面的匹配文件或文件夹

5 /**, 连续两个* 表示包含文件夹下的所有内容 (A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc")

6 (只有在没有 “/” 符号的前提下 , 才会wildcard匹配, 所以模版中不能加"/"这种路径操作) If the pattern does not contain a slash /, Git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file


注意: 我查了半天,没查到如何只包含某个文件夹下的特定文件的功能

参考:

https://stackoverflow.com/questions/19023550/how-do-i-add-files-without-dots-in-them-all-extension-less-files-to-the-gitign

https://git-scm.com/docs/gitignore




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值