gitignore - only allow certain extensions and files

一个人想着一个人 提交于 2019-11-27 13:35:41

问题


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.

Thanks in advance!


回答1:


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

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

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




回答2:


The most sophisticated method to achieve this

create .gitignore file in repository root, and add below lines to .gitignore file

*.*
!.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



来源:https://stackoverflow.com/questions/11852558/gitignore-only-allow-certain-extensions-and-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!