在我的.gitignore文件中添加bin、bin/、bin/*和bin/**有什么区别?我一直在使用bin/,但看看other .gitignore files (在eclipse file中,双星和单星甚至可以像这样一起使用:tmp/**/*这是怎么回事?)我看到前两种模式也被广泛使用。有人能解释一下这三者之间的区别吗?
发布于 2012-01-09 11:40:49
bin可以匹配任何名为“bin”的文件或目录。
bin/可以匹配任何名为'bin‘的目录,这实际上意味着它的所有内容,因为Git不会单独跟踪目录。
bin/*可以直接匹配任何bin/中的所有文件和目录。这将阻止Git自动在其子目录中查找任何文件,但是如果创建了一个bin/foo子目录,则此规则将与foo的内容不匹配。
bin/**匹配任何bin/目录及其所有子目录中的所有文件和目录。
"any“这个词在这里很重要,因为规则不是相对于存储库根目录的,并且适用于文件系统树中的任何位置。您必须以/ (或要取消忽略的!/ )开始规则,这意味着存储库的根目录,而不是系统的根目录,以便只匹配预期的内容。
WARNING:你应该永远不要单独使用诸如dir/*,/dir/**等规则,除非你也不忽略存在于该目录中的某些东西。在某些git gc、git stash等调用中省略星号或you could permanently lose a lot of data。
我真的不知道tmp/**/*要做什么。我最初认为它可以用来匹配tmp/的子目录中的文件,而不是tmp/本身中直接存在的文件。但一个简单的测试似乎表明这会忽略tmp/中的所有文件。
发布于 2013-12-05 13:04:08
bin和bin/的不同之处只在于后者只匹配一个目录。
bin/**/*和bin/**是一样的(根据@VonC的回答,很明显是从1.8.2开始)。
棘手的一点是,bin/和bin/**并不完全相同,这让我花了一个小时左右的时间。由于前者忽略了整个目录,后者忽略了其中的每个文件,而git几乎在所有情况下都不关心目录,所以通常没有区别。然而,如果你尝试使用!来取消忽略一个子路径,那么你会发现如果你忽略了父目录,git (嗯)就会忽略它!(同样,而不是目录内容)
这在示例中是最清楚的,因此对于新初始化的存储库,如下所示:
$ cat .gitignore
ignored-file
or-dir
dir-only/
!dir-only/cant-reinclude
dir-contents/**
!dir-contents/can-reinclude
$ mkdir or-dir dir-only dir-contents
$ touch file ignored-file or-dir/ignored-file dir-only/cant-reinclude dir-contents/can-reinclude存在以下未跟踪的文件:
$ git ls-files --other
.gitignore
dir-contents/can-reinclude
dir-only/cant-reinclude
file
ignored-file
or-dir/ignored-file但是您可以看到以下文件并未被忽略:
$ git ls-files --other --exclude-standard
.gitignore
dir-contents/can-reinclude
file如果你尝试加法,你会得到:
$ git add dir-only/cant-reinclude
The following paths are ignored by one of your .gitignore files:
dir-only/cant-reinclude
Use -f if you really want to add them.
fatal: no files added我认为这种行为是一个错误。(这一切都在git version 1.8.4.msysgit.0上)
发布于 2016-02-23 13:00:46
请注意,严格地说,git不跟踪目录,只跟踪文件。因此,无法添加目录,只能添加其内容。
然而,在.gitignore的上下文中,git假装理解目录的唯一原因是
如果文件的父目录被排除,则无法重新包含该文件。
https://git-scm.com/docs/gitignore#_pattern_format
这对排除模式意味着什么?让我们详细了解一下:
bin
这忽略了
名为bin.
bin的文件夹的内容
您可以通过添加后续bin条目将忽略的!文件和文件夹列入白名单,但不能将名为bin的文件夹的内容列入白名单
bin
!bin/file_in_bin # has no effect, since bin/ is blacklisted!
!bin/* # has no effect, since bin/ is blacklisted!
!file_in_bin # has no effect, since bin/ is blacklisted!
!bin # this worksbin/
与上面相同,只是它与名为bin的文件不匹配。添加一个尾随的/告诉git只匹配目录。
bin/*
这忽略了
包含在名为bin的文件夹的直接子文件夹的名为bin
bin/* # blacklists bin/file_in_bin and bin/subfolder/
!bin/subfolder/file_in_sub # has no effect, since bin/subfolder is blacklisted!
!bin # whitelists files named bin/bin, since bin/ itself is not blacklisted
!bin/ # has no effect, since bin/ itself is not blacklisted
!bin/file_in_bin # works since bin/ itself is not blacklisted
!file_in_bin # works too
!bin/subfolder # works (so implicitly whitelists bin/subfolder/file_in_sub)
!bin/subfolder/ # works just as well
!bin/* # works for file_in_bin and subfolder/bin/**
这忽略了
bin内子文件夹(任何级别的嵌套)的bin
bin/** # blacklists bin/file_in_bin and
# bin/subfolder/ and bin/subfolder/file_in_sub and
# bin/subfolder/2/ and bin/subfolder/2/file_in_sub_2
!bin/subfolder/file_in_sub # has no effect, since bin/subfolder is blacklisted
!bin/subfolder/2/ # has no effect, since bin/subfolder is blacklisted
!bin/subfolder/2/file_in_sub_2 # has no effect, since bin/subfolder is blacklisted
!bin/subfolder # works only in combinations with other whitelist entries,
# since all contents of subfolder are blacklisted (1)
!bin/file_in_bin # works since bin itself is not blacklisted
!bin/* # works for file_in_bin and subfolder; see (1)https://stackoverflow.com/questions/8783093
复制相似问题