git忽略所有文件,除了一个扩展名和文件夹结构

krcsximq  于 2023-11-15  发布在  Git
关注(0)|答案(5)|浏览(206)

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

  1. #ignore all kind of files
  2. *
  3. #except php files
  4. !*.php

字符串
有没有一种方法可以让git接受我的项目文件夹结构,同时只跟踪.php文件?
现在我似乎无法将文件夹添加到存储库中:

  1. vivo@vivoPC:~/workspace/motor$ git add my_folder/
  2. The following paths are ignored by one of your .gitignore files:
  3. my_folder
  4. Use -f if you really want to add them.
  5. fatal: no files added

oalqel3c

oalqel3c1#

这很简单,只需在.gitignore中添加另一个条目!my_folder即可

  1. #ignore all kind of files
  2. *
  3. #except php files
  4. !*.php
  5. !my_folder

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

编辑

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

  1. #ignore all kind of files
  2. *.*
  3. #except php files
  4. !*.php


这不会忽略任何没有扩展名的文件(例如:如果您有README而不是README.txt),并且会忽略名称中包含.的任何文件夹(例如:名为module.1的目录)。
FWIW、git doesn't track directories,因此无法指定目录与文件的忽略规则

展开查看全部
syqv5f0l

syqv5f0l2#

我也有类似的问题;我想将*.c列入白名单,但接受的答案对我不起作用,因为我的文件不包含“."。
对于那些想处理这个问题的人:

  1. # ignore everything
  2. *
  3. # but don't ignore files ending with slash = directories
  4. !*/
  5. # and don't ignore files ending with ".php"
  6. !*.php

字符串

v1l68za4

v1l68za43#

这适用于我(排除所有imgs文件夹内容,除了.gitkeep文件)

  1. /imgs/**/*.*
  2. !/imgs/**/.gitkeep

字符串

kokeuurv

kokeuurv4#

请注意,如果!没有效果,您可能排除了一个文件夹。从文档(强调我的):
一个可选的前缀“!“这否定了模式,任何被前一个模式排除的匹配文件将再次被包含。如果一个文件的父目录被排除,则不可能重新包含该文件. Git不会列出排除的目录,因为性能原因,所以包含文件上的任何模式都没有效果,无论它们是在哪里定义的。

d4so4syb

d4so4syb5#

找不到我要找的,所以加了一个对我有用的。
以下声明指出,
1.忽略所有内容(包括递归的子目录)
1.避免忽略特定文件
1.避免忽略所有子目录
现在如果你注意到step-3,你可以看到它是has cumulative effect of step-1, 2 declarations。也就是说,**当递归地取消忽略每个子目录时,忽略该子目录下的所有内容,除了在步骤2中忽略的几个文件。这将是carried out for each subdir that is being processed,因此声明的顺序/序列很重要。
范例:

  1. subdir1
  2. \subdir11\others.txt
  3. \subdir12\build.xml
  4. .gitignore
  5. subdir2
  6. \subdir21\resources\build.xml
  7. \subdir22\build.xml
  8. build.xml
  9. .gitignore

字符串
.gitignore文件应包含

  1. # !! NOTE: the sequence of declaration is important !!
  2. # Ignore Everything
  3. *
  4. # Exclude specific files from being ignored (override)
  5. !.gitignore
  6. !*.xml
  7. # overrides stated above, applies to all dir (root and subdirs - recursively)
  8. !**/


结果如下:

  1. subdir1\subdir12\build.xml
  2. subdir1\.gitignore
  3. subdir2\subdir21\resources\build.xml
  4. subdir2\subdir22\build.xml
  5. build.xml
  6. .gitignore


在此之后,if you still want to trim down or filter more从您已经过滤的那些中,add it next to the processing(记住:顺序很重要)

  1. # !! NOTE: the sequence of declaration is important !!
  2. # Ignore Everything
  3. *
  4. # Exclude specific files from being ignored (override)
  5. !.gitignore
  6. !build.xml
  7. # overrides stated above, applies to all dir (root and subdirs - recursively)
  8. !**/
  9. **/resources/**


现在,这将添加步骤4,这是**忽略 .xml*(仅在子目录中)其子目录路径包含中间resources目录,这些目录被过滤为结果,直到步骤3
结果如下:

  1. subdir1\subdir12\build.xml
  2. subdir1\.gitignore
  3. subdir2\subdir22\build.xml
  4. build.xml
  5. .gitignore


you can also ignore unignored files in step-2 only for subdirs(因此它们只包含在rootdir中,而不包含在子目录中)

  1. # !! NOTE: the sequence of declaration is important !!
  2. # Ignore Everything
  3. *
  4. # Exclude specific files from being ignored (override)
  5. !.gitignore
  6. !build.xml
  7. # overrides stated above, applies to all dir (root and subdirs - recursively)
  8. !**/
  9. **/resources
  10. **/.gitignore


除了步骤4的结果,这将排除所有子目录中的.gitignore,除了根目录。

展开查看全部

相关问题