git_gitignore的配置(exclude&include)/只跟踪指定目录下的指定文件

references

Git - gitignore Documentation

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. Put a backslash ("\") in front of
the first hash for patterns that begin with a hash.

•   Trailing spaces are ignored unless they are quoted with backslash ("\").

•   An optional prefix "!" which negates the pattern; any matching file excluded
by a previous pattern will become included again. It is not possible to
re-include a file if a parent directory of that file is excluded. Git doesn’t
list excluded directories for performance reasons, so any patterns on
contained files have no effect, no matter where they are defined. Put a
backslash ("\") in front of the first "!" for patterns that begin with a
literal "!", for example, "\!important!.txt".

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

•   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. Otherwise the pattern may also match at any level
below the .gitignore level.

•   If there is a separator at the end of the pattern then the pattern will only
match directories, otherwise the pattern can match both files and
directories.

•   For example, a pattern doc/frotz/ matches doc/frotz directory, but not
a/doc/frotz directory; however frotz/ matches frotz and a/frotz that is a
directory (all paths are relative from the .gitignore file).

•   An asterisk "*" matches anything except a slash. The character "?" matches
any one character except "/". The range notation, e.g.  [a-zA-Z], can be used
to match one of the characters in a range. See fnmatch(3) and the
FNM_PATHNAME flag for a more detailed description.

匹配文件还是目录?

我们将.gitignore中的每一行(每一个规则)称为一个匹配模式(pattern)

  • 如果某个匹配模式以/ (slash)结尾,那么该模式匹配的是目录(而不匹配文件))

  • 否则(仅目录名而不含结尾的/),那么既匹配目录,又匹配文件

    • 在windows11环境下测试,符号链接也会被匹配到

测试符号链接(windows)

在这里插入图片描述

  • 测试符号链接为symbolink
情况1:gitignore文件中没有symbolink(注释掉)
  • 发现,git可以正常检查symbolink的变更(存在)
    在这里插入图片描述
情况2:配置文件中写入symbolink
  • 发现符号链接的存在(变化)没有被git status提示
    在这里插入图片描述
情况3:gitignore写入symbolink\
  • 符号链接也可以被忽略规则symbolink\匹配到
    在这里插入图片描述

The pattern doc/frotz and /doc/frotz have the same effect in any .gitignore file.
In other words, a leading slash is not relevant if there is already a middle slash in the pattern.

single asterisk:*

"*" matches anything except a slash.
The character “?” matches
any one character except “/”.

double asterisk:**

**:with infinite depth.

关于已经被跟踪的文件/目录

  • 如果是这样,.gitignore无法对已经跟踪的目录(文件)产生作用
    • 但是也不是完全没有作用,例如,某个目录(testDir)在被跟踪提交后,才将他添加到.gitignore以阻止git 对 testDir中新增的文件的跟踪,但是已经被跟踪的那部分文件,git依然跟踪管理着它们
  • 您可以撤销跟踪,使得新配置的.gitignore能够生效.
  • 在这里插入图片描述
  • 注意对目录的撤销跟踪需要追加-r参数
  • 撤销还需要commit(相当于您对git给出最后的撤销确认)来使得.gitignore真正生效.
  • 在这里插入图片描述
  • 上图中的delete是repos区中的删除,不影响工作区

只跟踪指定目录下的指定文件

从文档中我们可以知道,某个目录如果被排除跟踪,那么其子目录将无法再次被强调跟踪
因此,如果您想要强调跟踪的文件(子目录)所在的目录不能够从目录的角度排除跟踪(以pathName的形式写入.gitignore中),而应该使用变通的方式pathName/*如此一来,排除的就不是目录,而是该目录下的所有子文件(子目录),这样其中的子项目可以被include again
(即,编写关于被完全排除的目录的子项的反排除规则(强调包含)是不起作用的)

其他pattern也有类似的限制,所以,使用pathName/*这种形式往往会更加灵活.

Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

2个小例子

涉及的语法以及含义:

  • 表达式格式->含义

DirName/*->dirListItems(DirName)
!item->includeItem(item)

  • 浅层
   #eclude everything except directory foo
   /*
   !/foo
  • 深层
 # for deeper specific path example:
   # exclude everything except directory foo/bar
    /*
    !/foo
    /foo/*
    !/foo/bar

另一实例

仅排除指定目录test,但是要保留该目录中的.json文件
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
关于符号链接(symbolic/Junction)引入的目录,在里面设置的.gitignore将无法生效,您只可以在git管理的根目录或者非符号链接的子目录下创建有效的.gitignore

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值