gitignore

gitignore

Specifies(指定) intentionally(有意地) untracked files to ignore

不跟踪指定的文件的变更状态

PATTERN(模式) FORMAT(格式)

  • A blank line matches no files, so it can serve as a separator for readability.

空行不匹配任何文件


  • A line starting with # serves as a comment(注释).

需要注意的是注释单独占一行并且以 # 开头

✅正确
# CocoaPods
/ios/Pods/

❌错误
# ⚠️注释没有单独占一行
/ios/Pods/	# CocoaPods

  • The slash / is used as the directory separator. Separators may occur at the beginning, middle or end of the .gitignore search pattern.

/ 用来作为目录分隔符,/ 可能出现在 .gitignore 中搜索模式的开头、中间和结尾

# 开头、中间、结尾
/ios/Pods/

  • If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself.

如果 / 出现在开头或中间,根据 .gitignore 文件所在位置匹配,匹配目录或文件。

# 开头和中间有分隔符
# 根据 .gitignore 文件所在位置匹配
/ios/Pods

示例:

✅正确匹配,会忽略
# .gitignore 和 ios 同级目录,Pods是目录
.gitignore
ios/
  Pods/

✅正确匹配,会忽略
# .gitignore 和 ios 同级目录,Pods是文件
.gitignore
ios/
  Pods
  
❌匹配不到,不会忽略
# .gitignore 和 ios 不同级
.gitignore
src/
  ios/
    Pods/

  • If there is a separator at the end of the pattern then the pattern will only match directories.

如果 / 出现在结尾,只会匹配目录

# 结尾
node_modules/

# 结尾
ios/Pods/

示例

✅正确匹配,会忽略
.gitignore
ios/
  Pods/
  
❌匹配不到,不会忽略  
# ⚠️注意注意注意 Pods 是文件,所以匹配不到,不会忽略
.gitignore
ios/
  Pods 

总结:

情况一:/ 出现在开头、/ 出现在中间、/ 出现在开头和中间

# 出现在开头
/ios

# 出现在中间
ios/Pods

# 出现在开头和中间
/ios/Pods

根据 .gitignore 文件所在位置匹配,匹配目录和文件

情况二:/ 出现在结尾

node_modules/

全局匹配,只匹配目录

✅正确匹配,会忽略
.gitignore
node_modules/

✅正确匹配,会忽略
.gitignore
src/
  node_modules/

情况三:情况一和情况二结合

# 出现在中间 和 出现在结尾
ios/Pods/

需要同时满足两个条件:根据 .gitignore 文件所在位置匹配;只匹配目录。

✅正确匹配,会忽略
.gitignore
ios/
  Pods/
  
❌匹配不到,不会忽略  
# ⚠️注意注意注意 Pods 不是目录,所以匹配不到,不会忽略
.gitignore
ios/
  Pods
  
❌匹配不到,不会忽略  
# ⚠️注意注意注意 .gitignore 和 ios 不同级,所以匹配不到,不会忽略
.gitignore
src/
  ios/
    Pods/
posted @ 2021-09-07 18:43  蓦然回首!  阅读(87)  评论(0编辑  收藏  举报