使用 Simulink Embedded Coder 时,项目目录下会生成多个子文件夹。这些子文件夹中既有自动生成的代码,如 .c 和 .h 文件,也有大量临时文件。用 Git 做版本控制时,我们希望把自动生成的 .c 和 .h 文件纳入版本管理,同时忽略其他临时文件。在编写 gitignore 的过程中,我发现不同的写法会得到不同的结果。

匹配规则

  • ! 对模式取反,之前被某个模式排除的匹配文件会重新被包含进来。
    • 如果模式本身以字面字符 "!" 开头,要在第一个 "!" 前面加反斜杠("\"),例如 "\!important!.txt"。
  • / 目录分隔符。
    • 如果分隔符出现在模式的开头或中间(或两处都有),该模式相对于这个 .gitignore 文件所在的目录层级;否则,该模式也可以匹配 .gitignore 所在层级之下任意层级的路径。
    • 如果分隔符出现在模式末尾,该模式只匹配目录;否则既可以匹配文件,也可以匹配目录。
    • 例如,模式 doc/frotz/ 匹配 doc/frotz 目录,但不匹配 a/doc/frotz 目录;而 frotz/ 会匹配 frotz 和 a/frotz 这两个目录(所有路径都相对于 .gitignore 文件)。
  • * 星号匹配除 "/" 以外的任意内容。
  • ? 匹配除 "/" 以外的任意单个字符。
  • [a-zA-Z] 范围写法匹配该范围内的某一个字符。
  • ** 两个连续的星号匹配完整路径名,包括 "/"。

! 是怎样起作用的

大家都知道,可选前缀 "!" 用来对模式取反。不过,只有在该模式的父文件夹没有被忽略时它才有效,例如:

#wrong 
#ignore the folder slprj
slprj/

#the following negate pattern will not work, because slprj/ is ignored
!slprj/**/*.c
#correct
#ignore the contents of slprj
slprj/*

#negate one subfolder of slprj
!slprj/ert/

#ignore the contents of subfolders in slprj/ert
slprj/ert/*/*

#negate the following pattern (in slprj/ert/ or its subfolder)
!slprj/**/*.c

一个用于 Simulink Coder 项目的 .gitignore 示例

slprj/*
!slprj/ert/
slprj/ert/*/*
*_*_rtw/*
!*_*_rtw/*.c
!*_*_rtw/*.h
!slprj/**/*.c
!slprj/**/*.h

参考

gitignore – Specifies intentionally untracked files to ignore(指定需要有意忽略的未跟踪文件)


0 条评论

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注