git gitignore - 只允许某些扩展名和文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11852558/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:17:48  来源:igfitidea点击:

gitignore - only allow certain extensions and files

gitgitignore

提问by jleck

not sure why this isn't working, as I've done this loads! Maybe I'm having a bad day...

不知道为什么这不起作用,因为我已经完成了这些工作!也许我今天过得很糟糕...

I'm using the following code to ignore all files except for certain filenames and extension

我正在使用以下代码忽略除某些文件名和扩展名之外的所有文件

*
!.gitattributes
!.gitignore
!readme.md
!.gitkeep
!*.php

For some reason, its only allowing me to commit the .gitignore and readme.md, even though I have php files in subfolders etc. Is there anything wrong with it? Just fyi, I'm using "git add -A" to pick up the files to commit.

出于某种原因,它只允许我提交 .gitignore 和 readme.md,即使我在子文件夹等中有 php 文件。它有什么问题吗?仅供参考,我正在使用“git add -A”来获取要提交的文件。

Thanks in advance!

提前致谢!

回答by eckes

The solution is to tell Git not to ignore sub directories:

解决方案是告诉 Git不要忽略子目录

*
!.gitattributes
!.gitignore
!readme.md
!.gitkeep
!*.php
!*/

Otherwise, only the *.phpfiles in the first directory level will be accepted and all sub directories will be ignored.

否则,只*.php接受第一级目录中的文件,而忽略所有子目录。

回答by Smaranjit

The most sophisticated method to achieve this

实现这一目标的最复杂方法

create .gitignorefile in repository root, and add below lines to .gitignorefile

.gitignore在存储库中创建文件root,并将以下几行添加到.gitignore文件中

*.*
!.gitattributes
!.gitignore
!readme.md
!.gitkeep
!*.php

this will include all specified file from directory and subdirectory recursively.

这将递归地包括目录和子目录中的所有指定文件。

tested on

测试

git version 2.12.2.windows.2

git 版本 2.12.2.windows.2