gitignore:忽略文件夹层次结构中除一种特定文件类型之外的所有文件
我想忽略文件夹下面和文件夹中的所有文件,但可能位于文件夹层次结构中某处的特定文件类型除外:
示例
/Test
/Test/unknown/folder/structure/below
现在我想忽略 Test
文件夹中及其下面的所有文件,除了某个名为 layout.css
的 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 应该忽略
/Test/fileto.ignore
/Test/another/folder/ig.nore
/Test/in/a/unknown/folder/ignore.me
我的 .gitignore 文件不起作用:
Test/
!layout.css
有什么建议吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以通过将包含以下内容的
.gitignore
文件放入Test/
目录中来使您的示例正常工作。I was able to get your example to work by putting a
.gitignore
file in theTest/
directory with the following contents.这是一个相当老的问题,但由于我现在仍在努力解决这个问题,我想我会分享该问题的实际解决方案。
问题是,虽然您不能在 Git 中提交空目录,就好像它们被忽略一样,但此规则不适用于
.gitignore
。来自 https://git-scm.com/docs/gitignore
** 匹配里面的所有内容 - 字面上的所有内容 - 文件和目录。
这引出了 gitignore 文档中的另一点:
现在,假设我们有以下 dir 结构:
我们想要忽略 cache/ 中除 README 文件之外的所有文件。
如果我们像这样指定 gitignore:
Git 将忽略除
/cache/README
之外的所有内容。另一个 README 不会显示,因为它的目录/cache/dir
被/cache/**
排除,并且!README
不会显示甚至适用于它。为了解决这个问题,我们需要指定 gitignore 如下:
Quite an 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
.From https://git-scm.com/docs/gitignore
** Matches everything inside - literally everything - files AND directories.
This leads us to another point in gitignore docs:
Now, let's say we have a following dir structure:
And we want to ignore all files inside cache/ except for README files.
If we specify gitignore like this:
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:
为了实现您想要的目标,您需要使用一些否定排除。
基本思想是您需要排除您想要忽略的任何文件的每个父目录。
因此,如果您希望将 /Test/in/a/unknown/folder/layout.css 添加到您的 git 存储库中,那么您必须取消忽略 /Test/、/Test/in/、/Test/in/a /、/Test/in/a/unknown/ 和 /Test/in/a/unknown/folder/。
然后,当您最终到达包含一些被忽略的文件和一些未被忽略的文件的目录时,您需要单独指定每个文件,如下所示:
因此,当您运行 $ git add-all 时,您将看到您想要的结果:
注意:您可以在 git add-all 是将文件添加到 git 存储库的最佳方式的解释。 href="http://lexsheehan.blogspot.com/2014/05/git-add-update.html" rel="noreferrer">http://lexsheehan.blogspot.com/2014/05/git-add-update .html
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/.
Then, when you finally get to the directory with some ignored and some unignored files, you'll need to specify each individually as follows:
So when you run
$ git add-all
you'll see your desired results: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