git忽略除一个扩展名和文件夹结构之外的所有文件

viv*_*ino 26 git gitignore

这是我的.gitignore:

#ignore all kind of files
*
#except php files
!*.php
Run Code Online (Sandbox Code Playgroud)

我想要的只是忽略除了.php之外的所有文件,但是这个.gitignore我也忽略了文件夹...

有没有办法告诉git接受我的项目文件夹结构,同时只保留.php文件的轨道?

好像现在我无法在我的仓库中添加文件夹:

vivo@vivoPC:~/workspace/motor$ git add my_folder/
The following paths are ignored by one of your .gitignore files:
my_folder
Use -f if you really want to add them.
fatal: no files added
Run Code Online (Sandbox Code Playgroud)

mu *_*u 無 28

这很简单,只需添加另一个项目!my_folder.gitignore

#ignore all kind of files
*
#except php files
!*.php
!my_folder
Run Code Online (Sandbox Code Playgroud)

最后一行将特别注意my_folder,并且不会忽略其中的任何php文件; 但由于第一种模式,其他文件夹中的文件仍将被忽略*.

编辑

我想我误读了你的问题.如果要忽略文件以外的所有.php文件,可以使用

#ignore all kind of files
*.*
#except php files
!*.php
Run Code Online (Sandbox Code Playgroud)

这不会忽略任何没有扩展名的文件(例如:如果你有README没有README.txt),并且会忽略.名称中包含a的任何文件夹(例如:名为directory module.1).

FWIW,git不跟踪目录,因此无法为目录vs文件指定忽略规则

  • @vivoconunxino没有那不可能; git根本不跟踪文件夹.gitignore中的条目是模式,并且忽略与其匹配的任何文件名或文件路径(表示目录). (3认同)

kal*_*nar 8

我有一个类似的问题。我想将其列入白名单*.c,但是接受的答案对我而言不起作用,因为我的文件中不包含“。”。

因此,对于那些想要处理的人:

# ignore everything
*

# but don't ignore files ending with slash = directories
!*/

# and don't ignore files ending with ".php"
!*.php
Run Code Online (Sandbox Code Playgroud)

  • 完美的!(添加 `!.gitignore`) (4认同)

max*_*max 7

这对我有用(不包括除 .gitkeep 文件之外的所有 imgs 文件夹内容)

/imgs/**/*.*
!/imgs/**/.gitkeep
Run Code Online (Sandbox Code Playgroud)


Roa*_*ald 6

请注意,如果!没有效果,您可能排除了某个文件夹。来自文档(强调我的):

\n
\n

可选前缀“!” 这否定了模式;先前模式排除的任何匹配文件\n将再次包含在内。如果文件的父目录被排除,\n则无法重新包含该文件。出于性能原因,Git\xe2\x80\x99t 不会列出\n排除的目录,因此所包含文件上的任何模式都无效,\n无论它们在何处定义。

\n
\n