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

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

git ignore all files except one extension and folder structure

gitgitignore

提问by vivoconunxino

This is my .gitignore:

这是我的 .gitignore:

#ignore all kind of files
*
#except php files
!*.php

All I want is to ignore all kind of files except the .php ones, but with this .gitignore I'm also ignoring folders...

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

Is there a way to tell git to accept my project folder structure while keeping the track only of the .php files?

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

It seems like now I can't add folders to my repo:

现在似乎我无法将文件夹添加到我的存储库中:

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

回答by u1860929

This is simple, just add another entry !my_folderin your .gitignore

这很简单,只需!my_folder在您的.gitignore

#ignore all kind of files
*
#except php files
!*.php
!my_folder

The last line will take special care of my_folder, and will not ignore any php files within it; but files within other folders will still be ignored because of the first pattern of *.

最后一行会特别注意my_folder,不会忽略其中的任何 php 文件;但其他文件夹中的文件仍将被忽略,因为*.

EDIT

编辑

I think I misread your question. If you want to ignore all files except .phpfiles, you can use

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

#ignore all kind of files
*.*
#except php files
!*.php

This will not ignore any file which doesn't have an extension (example: if you have READMEand not README.txt), and will ignore any folder with a .in its name (example: directory named module.1).

这不会忽略任何没有扩展名的文件(例如:如果您有README并且没有README.txt),并且会忽略.名称中带有 a 的任何文件夹(例如:名为 的目录module.1)。

FWIW, git doesn't track directories, and hence there is no way to specify ignore rules for directory vs file

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

回答by kalnar

I had a similar problem; I wanted to whitelist *.c, but the accepted answer didn't work for me, because I had files that didn't contain ".".

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

So for those who want to handle that:

所以对于那些想要处理这个问题的人:

# ignore everything
*

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

# and don't ignore files ending with ".php"
!*.php

回答by max

This works for me (excludes all imgs folder content except .gitkeep files)

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

/imgs/**/*.*
!/imgs/**/.gitkeep