简体   繁体   English

.gitignore 忽略与某些顶级子目录关联的嵌套子目录中具有某些扩展名的文件之外的所有内容

[英].gitignore to ignore everything aside from files with certain extensions in nested subdirectories associated with some top level subdirectories

I would like for git to ignore all files except those with a few different extensions buried somewhere within a specific set of subdirectories.我希望 git 忽略所有文件,除了那些在特定子目录中某处埋有几个不同扩展名的文件。

For examples, say I have a directory with the following sub-directories:例如,假设我有一个包含以下子目录的目录:

subdir1
subdir2
subdir3
subdir4
subdir5
...

Each subdirectory contains an arbitrary number of nested sub-directories.每个子目录包含任意数量的嵌套子目录。 Files with extension .txt or .pdf may be present in some of those nested sub-directories.扩展名为.txt.pdf的文件可能存在于其中一些嵌套子目录中。 I would like to have the repo track only the .txt and .pdf files somewhere within subdir2 and subdir3 and no other files.我想让 repo 只跟踪subdir2subdir3中某处的.txt.pdf文件,而没有其他文件。

Please explain why your solution works.请解释为什么您的解决方案有效。

If you want to ignore ALL files expect all .pdf and .txt files in the directory subdir2/ and subdir3/ , add following to gitignore :如果您想忽略所有文件,希望目录subdir2/subdir3/中的所有.pdf.txt文件,请将以下内容添加到gitignore

# Ignore everything
*
!*/

# But not these files
!.gitignore
!subdir2/**/*.pdf
!subdir2/**/*.txt
!subdir3/**/*.pdf
!subdir3/**/*.txt

subdir/** matches all files and directories in any subdir/ directory and all of its subdirectories. subdir/**匹配任何subdir/目录及其所有子目录中的所有文件目录 The ! ! negates the pattern where follows.否定后面的模式。 And so you can ignore all except the .pdf and .txt files in the wished directories.因此,您可以忽略所需目录中的.pdf.txt文件以外的所有文件。

The folder-structure is as follow:文件夹结构如下:

|-- folder
    |-- .git
    |-- .gitignore
    |-- subdir1/
        |-- <folder content>
    |-- subdir2/
        |-- <folder content>
    |-- subdir3/
        |-- <folder content>
    |-- subdir4/
        |-- <folder content>
    |-- subdir5/
        |-- <folder content>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM