git忽略除一个扩展名和文件夹结构以外的所有文件 [英] git ignore all files except one extension and folder structure

查看:108
本文介绍了git忽略除一个扩展名和文件夹结构以外的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的.gitignore:

This is my .gitignore:

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

我只想忽略除.php文件以外的所有类型的文件,但是有了.gitignore,我也忽略了文件夹...

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

是否有一种方法可以告诉git接受我的项目文件夹结构,同时仅跟踪.php文件?

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

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

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

推荐答案

这很简单,只需在.gitignore

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

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

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

编辑

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

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

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

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

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

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

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

这篇关于git忽略除一个扩展名和文件夹结构以外的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆