git 如何将不带点的文件(所有无扩展名的文件)添加到 gitignore 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19023550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add files without dots in them (all extension-less files) to the gitignore file?
提问by The Unfun Cat
Like the title says, is it possible to add "files without dots in them" to the gitignore file?
就像标题所说的那样,是否可以将“没有点的文件”添加到 gitignore 文件中?
I imagine this would take care of all those bothersome extensionless files.
我想这会处理所有那些麻烦的无扩展文件。
回答by VonC
You can try a combination similar to:
您可以尝试类似于以下的组合:
*
!/**/
!*.*
That gitignore
exclusion rule(a negated pattern) should ignore all files, except the ones with an extension.
该gitignore
排除规则(否定模式)应忽略所有文件,但带有扩展名的文件除外。
As mentioned belowby Mad Physicist, the rule is:
正如Mad Physicist在下面提到的,规则是:
It is not possible to re-include a file if a parent directory of that file is excluded.(*
)
(*
: unless certain conditions are met in git 2.?+, see below)
如果排除了该文件的父目录,则无法重新包含该文件。(*
)
(*
: 除非在 git 2.?+ 中满足某些条件,请参见下文)
That is why !/**/
is important (white-listing the parent folders recursively) if we want to white-list files.
!/**/
如果我们想将文件列入白名单,这就是为什么很重要(递归地将父文件夹列入白名单)。
I mentioned that same rule in similar cases like:
我在类似的情况下提到了相同的规则,例如:
- Gitignore all except one folder and all its content - regardless of the nesting level
- Git except a sub directory and it's files of a ignored directory
- Gitignore exclude certain files in all subdirectories
As Jakub Nar?bskicomments, you might not want to ignore allextensionless files.
正如Jakub Nar?bski评论的那样,您可能不想忽略所有无扩展名的文件。
My advice:
我的建议:
- add first the extensionless file that matters
- then edit your
.gitignore
as shown above: the already versioned files won't be ignored (even if they don't have an extension). All the others will be ignored.
- 首先添加重要的无扩展名文件
- 然后
.gitignore
如上所示进行编辑:不会忽略已经版本化的文件(即使它们没有扩展名)。所有其他人都将被忽略。
For any future extensionless files that you would want to version:
对于您希望版本化的任何未来无扩展文件:
git add -f -- myFile
Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.
请注意,对于 git 2.9.x/2.10(2016 年中期?),如果路径 re-included 中没有通配符,则如果排除该文件的父目录,则可能会重新包含该文件。
Nguy?n Thái Ng?c Duy (pclouds
)is trying to add this feature:
Nguy?n Thái Ng?c Duy ( pclouds
)正在尝试添加此功能:
- commit 506d8f1for git v2.7.0, reverted in commit 76b620dgit v2.8.0-rc0
- commit 5e57f9cgit v2.8.0-rc0,... reverted(!) in commit 5cee3493git 2.8.0-rc4.
- 为 git v2.7.0提交 506d8f1,在提交 76b620dgit v2.8.0-rc0 中恢复
- 提交 5e57f9cgit v2.8.0-rc0,...在提交 5cee3493git 2.8.0-rc4 中恢复(!)。
However, since one of the rules to re-inclusion was:
但是,由于重新纳入的规则之一是:
The directory part in the re-include rules must be literal (i.e. no wildcards)
重新包含规则中的目录部分必须是文字(即没有通配符)
This wouldn't have worked here anyway.
无论如何,这在这里是行不通的。
回答by Mad Physicist
*
!*/
!*.*
*
tells git to ignore everything.
*
告诉 git 忽略一切。
!*/
then unignores anything that is a directory. This is crucial.
!*/
然后忽略任何目录。这是至关重要的。
!*.*
unignores all files with an extension.
!*.*
取消忽略所有带有扩展名的文件。
Without the !*/
rule, directories without a .
in the name would not be listed and none of your desired files would be added outside the root folder.
如果没有该!*/
规则,则.
不会列出名称中没有 a 的目录,并且不会将您想要的任何文件添加到根文件夹之外。
For reference, read these two sections in the .gitignore documentationstand out:
作为参考,请阅读.gitignore 文档中的这两部分脱颖而出:
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".
If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in Git).
一个可选的前缀“!” 这否定了模式;任何被先前模式排除的匹配文件将再次包含在内。如果排除了该文件的父目录,则无法重新包含该文件。出于性能原因,Git 不会列出排除的目录,因此包含文件的任何模式都不起作用,无论它们在哪里定义。在第一个“!”前面放一个反斜杠(“\”)对于以文字“!”开头的模式,例如,“!important!.txt”。
如果模式以斜杠结尾,则出于以下描述的目的将其删除,但它只会找到与目录的匹配项。换句话说, foo/ 将匹配目录 foo 和它下面的路径,但不会匹配常规文件或符号链接 foo (这与 pathspec 在 Git 中的一般工作方式一致)。
回答by Antranig Avakian
In my folders there are lots of files with *.c, *.h, *.txt, *.csv
etc. extensions and binary files without any extension. So I needed to ignore all files execpt *.c,*.h
and .gitignore
, So this works for me, from the .gitignore
example:
在我的文件夹中有很多带有*.c, *.h, *.txt, *.csv
等扩展名的文件和没有任何扩展名的二进制文件。所以我需要忽略所有文件 execpt*.c,*.h
和.gitignore
,所以这对我有用,来自.gitignore
示例:
*/* #ignore all files in each directory
!*/*.c #unignore .c files in each directory
!*/*.h #unignore .h header files in each directory
!.gitignore #unignore .gitignore