git gitignore:忽略文件夹层次结构中的所有文件,除了一种特定的文件类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7803689/
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
gitignore: Ignore all files in folder hierarchy except one specific filetype
提问by J?rg
I'd like to ignore all files below and in a folder except a specific filetype that could be somewhere in the folders hierarchy:
我想忽略下面和文件夹中的所有文件,但可能位于文件夹层次结构中某处的特定文件类型除外:
Example
例子
/Test
/Test/unknown/folder/structure/below
Now I'd like to ignore all files in and below the Test
folder except a certain css file named layout.css
, e.g.:
现在我想忽略文件Test
夹中和文件夹下的所有文件,除了名为 的某个 css 文件layout.css
,例如:
/Test/layout.css
/Test/fileto.ignore
/Test/another/folder/ig.nore
/Test/in/a/unknown/folder/layout.css
/Test/in/a/unknown/folder/ignore.me
.gitignore should ignore
.gitignore 应该忽略
/Test/fileto.ignore
/Test/another/folder/ig.nore
/Test/in/a/unknown/folder/ignore.me
My .gitignore file does not work:
我的 .gitignore 文件不起作用:
Test/
!layout.css
Any suggestions?
有什么建议?
回答by David Alber
I was able to get your example to work by putting a .gitignore
file in the Test/
directory with the following contents.
通过将一个.gitignore
文件放入Test/
目录中,我能够让您的示例工作,其中包含以下内容。
*
!*/
!.gitignore
!layout.css
回答by l3x
In order to accomplish what you want, you'll need to use some negative exclusions.
为了完成你想要的,你需要使用一些否定排除。
The basic idea is that you need to exclude every parent directory of any file that you want unignored.
基本思想是您需要排除您想要忽略的任何文件的每个父目录。
So, if you want /Test/in/a/unknown/folder/layout.css to be added to your git repo, then you'll have to unignore /Test/, /Test/in/, /Test/in/a/, /Test/in/a/unknown/, and /Test/in/a/unknown/folder/.
因此,如果您想将 /Test/in/a/unknown/folder/layout.css 添加到您的 git 存储库中,那么您必须忽略 /Test/、/Test/in/、/Test/in/a /、/Test/in/a/unknown/ 和 /Test/in/a/unknown/folder/。
Then, when you finally get to the directory with some ignored and some unignored files, you'll need to specify each individually as follows:
然后,当您最终到达包含一些忽略文件和一些未忽略文件的目录时,您需要按如下方式单独指定每个文件:
# Fille: .gitignore
!Test/
Test/*
!Test/layout.css
Test/fileto.ignore
!Test/another/
Test/another/*
!Test/another/folder/
Test/another/folder/*
!Test/in/
Test/in/*
!Test/in/a/
Test/in/a/*
!Test/in/a/unknown/
Test/in/a/unknown/*
!Test/in/a/unknown/folder/
Test/in/a/unknown/folder/ignore.me
!Test/in/a/unknown/folder/layout.css
So when you run $ git add-all
you'll see your desired results:
所以当你运行时,$ git add-all
你会看到你想要的结果:
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: Test/in/a/unknown/folder/layout.css
new file: Test/layout.css
Note: You can find an explanation of why git add-all
is the best way to add files to a git repo at http://lexsheehan.blogspot.com/2014/05/git-add-update.html
注意:您可以git add-all
在http://lexsheehan.blogspot.com/2014/05/git-add-update.html找到有关为什么是将文件添加到 git 存储库的最佳方式的解释
回答by Aleksander Stelmaczonek
Quite old question but since I was struggling with this issue even now I figured I would share the actual solution to the problem.
很老的问题,但由于我现在还在为这个问题苦苦挣扎,我想我会分享这个问题的实际解决方案。
The thing is while you cannot commit empty directories in Git as if they were ignored, this rule does not apply to .gitignore
.
问题是,虽然您不能在 Git 中提交空目录,就好像它们被忽略一样,但此规则不适用于.gitignore
.
From https://git-scm.com/docs/gitignore
来自https://git-scm.com/docs/gitignore
A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.
A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.
尾随的“/**”匹配里面的所有内容。例如,“abc/**”匹配目录“abc”中的所有文件,相对于 .gitignore 文件的位置,具有无限深度。
斜杠后跟两个连续的星号,然后斜杠匹配零个或多个目录。例如,“a/**/b”匹配“a/b”、“a/x/b”、“a/x/y/b”等。
** Matches everything inside - literaly everything - files AND directories.
** 匹配里面的所有内容 - 字面上的所有内容 - 文件和目录。
This leads us to another point in gitignore docs:
这将我们引向 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 中的一般工作方式一致)。
Now, let's say we have a following dir structure:
现在,假设我们有以下目录结构:
/path/to/git/repo
|-- .gitignore
|-- /cache
|-- somefile
|-- README
|-- /dir
|-- somefile2
|-- README
And we want to ignore all files inside cache/ except for README files.
我们想忽略 cache/ 中的所有文件,除了 README 文件。
If we specify gitignore like this:
如果我们像这样指定 gitignore:
/cache/**
!README
Git will ignore everything except for /cache/README
. The other README won't show up because its directory /cache/dir
was excluded with /cache/**
and !README
won't even apply to it.
To solve the issue we need to specify gitignore as this:
Git 将忽略除/cache/README
. 其他自述不会显示出来,因为它的目录/cache/dir
中排除与/cache/**
和!README
甚至不会适用于它。为了解决这个问题,我们需要将 gitignore 指定为:
# Ignore everything inside cache/ dir
/cache/**
# Except for subdirectories(won't be commited anyway if there is no commited file inside)
!/cache/**/
# And except for README files
!README